Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calculating player direction to point

Discussion in 'Scripting' started by SVKsuli, Oct 19, 2018.

  1. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    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.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    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.
     
  3. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    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 :(
     
    Last edited: Oct 19, 2018
  4. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Ok so i already got it... I dont know what im doing wrong before but it work how you write Antistone, thank you :)

    Here is working code:
    Code (CSharp):
    1.     private float PointToAngle(Vector3 point, Vector3 direction)
    2.     {
    3.         float angle = 0;
    4.  
    5.         Vector3 fromPlayerToPoint = point - localPos;
    6.         Vector3 pop = Vector3.ProjectOnPlane(fromPlayerToPoint, transform.up);
    7.         float pointOrientation = Vector3.SignedAngle(transform.right, fromPlayerToPoint, transform.up);
    8.  
    9.         if(pointOrientation <= 90)
    10.             angle = Vector3.SignedAngle(direction, fromPlayerToPoint, transform.up);
    11.         else
    12.             angle = (Vector3.SignedAngle(direction, fromPlayerToPoint, transform.up) * -1) + 360;
    13.  
    14.         return angle;
    15.     }