// Returns the local direction of V2 relative to V1
public static Vector2 GetLocalDirection(Vector2 V1, Vector2 V2)
{
// Normalize V1 to ensure it represents forward direction
Vector2 forward = V1.normalized;
// Calculate the right-hand perpendicular vector
Vector2 right = new Vector2(forward.y, -forward.x);
// Compute the difference vector from V1 to V2
Vector2 difference = V2 - V1;
// Project the difference onto the forward and right vectors
float localY = Vector2.Dot(difference, forward); // Projection onto forward
float localX = Vector2.Dot(difference, right); // Projection onto right
// Return the local direction
return new Vector2(localX, localY);
}
image.png