Created
April 29, 2020 07:56
-
-
Save SiarheiPilat/932524bad41995c46a534648faac1fb1 to your computer and use it in GitHub Desktop.
Computes distances between vectors faster than Vector3.Distance() or Vector2.Distance()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Vector3.Distance(a,b) is the same as (a-b).magnitude | |
/// Since computing squared magnitudes is faster than Vector3.magnitude, (a - b).sqrMagnitude is more performant than Vector3.Distance | |
/// </summary> | |
public class PerformanceEnhancer : MonoBehaviour | |
{ | |
public static float Distance(Vector3 a, Vector3 b) | |
{ | |
return (a - b).sqrMagnitude; | |
} | |
public static float Distance(Vector2 a, Vector2 b) | |
{ | |
return (a - b).sqrMagnitude; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment