Calculating player direction to point

Hello, I need help with solving this problem.
I have random vector3 world points collected from ray and i need to know in which side is player rotated to this point (left or right) from player direction.
Its in 2D and player move in x and y axis… Here is example picture for better imagination:

Its look easy but its really insidious problem (for me), cant find nothing helpful in google.

I think you’d want to do something like this:

First, you need to know the point’s coordinates relative to the player’s coordinates. You can do that by subtracting their positions.

Vector3 fromPlayerToPoint = point.transform.position - player.transform.position;

If it’s possible the point is above or below the player (relative to the player’s viewpoint), you probably want to project this onto the player’s horizontal plane so you can determine just the horizontal angle without the vertical angle complicating things. You can do that using ProjectOnPlane, something like this:

fromPlayerToPoint = Vector3.ProjectOnPlane(fromPlayerToPoint, player.transform.up);

Then we can compare that direction to the player’s facing direction using SignedAngle:

float angleToPoint = Vector3.SignedAngle(player.transform.forward, fromPlayerToPoint, player.transform.up);

Angles between 45 and -45 will be “in front” of the player, etc.

thx for answer… ProjectOnPlane is new to me, but it look like its always working with origin direction not player. and angleToPoint always return 0°.

Edit: Sory, yes angle could tell me in which side is point relative to player orientation correctly, but problem is when point is in front of player in 0°. It return always different angles.

I like to use this for calculate angle of point to player direction with full 360° with SignedAngle but it work only to 180° ,then i need to know if point is in left or right to player direction…
I try to calculate this with “Quaternion.FromToRotation(direction, point - localPos).eulerAngles.z” …It work exactly how i want but with same problem like your example with ProjectOnPlane, if point is in front of player then angle is returned wrong :frowning:

Ok so i already got it… I dont know what im doing wrong before but it work how you write Antistone, thank you :slight_smile:

Here is working code:

    private float PointToAngle(Vector3 point, Vector3 direction)
    {
        float angle = 0;

        Vector3 fromPlayerToPoint = point - localPos;
        Vector3 pop = Vector3.ProjectOnPlane(fromPlayerToPoint, transform.up);
        float pointOrientation = Vector3.SignedAngle(transform.right, fromPlayerToPoint, transform.up);

        if(pointOrientation <= 90)
            angle = Vector3.SignedAngle(direction, fromPlayerToPoint, transform.up);
        else
            angle = (Vector3.SignedAngle(direction, fromPlayerToPoint, transform.up) * -1) + 360;

        return angle;
    }