Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created April 29, 2020 07:56
Show Gist options
  • Save SiarheiPilat/932524bad41995c46a534648faac1fb1 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/932524bad41995c46a534648faac1fb1 to your computer and use it in GitHub Desktop.
Computes distances between vectors faster than Vector3.Distance() or Vector2.Distance()
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