Unity calculates the distance between 2 objects (between 2 3D vectors)

 

  public float GetDistance(Vector3 startPoint, Vector3 endPoint)

    {

        float distance = (startPoint - endPoint).magnitude;

        return distance;

    }

 

 

Method 2

    public double GetDistance(Vector3 startPoint, Vector3 endPoint)

    {

        double x = System.Math.Abs(endPoint.x - startPoint.x);

        double y = System.Math.Abs(endPoint.y - startPoint.y);

        return Math.Sqrt(x * x + y * y);

 

    }

Guess you like

Origin blog.csdn.net/qq_39646949/article/details/129971217