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. Dismiss Notice

Question How can I make a game object check if another object is to its left or its right?

Discussion in 'Editor & General Support' started by Megalythyx, Aug 24, 2023.

  1. Megalythyx

    Megalythyx

    Joined:
    Aug 24, 2023
    Posts:
    3
    I want my "enemy" game object to to be able to tell if my player is to the enemy's left or right (relative to the enemy's rotation). Like this:
    Code (CSharp):
    1. if (player is to my left)
    2.  
    3. {do xyz}
    4.  
    5. else if (player is to my right)
    6.  
    7. {do zyx}
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Generally the method to compare directions is with the dot product of two vectors: https://docs.unity3d.com/ScriptReference/Vector3.Dot.html

    You could figure this out by finding the direction from the enemy to the player, and then comparing said direction to the enemy's
    transform.right
    , perhaps. If the value is positive, the player is to the right of the enemy, if negative, it's to the left.

    That said, what's your actual end goal here? You might not need branching logic for this.
     
  3. Megalythyx

    Megalythyx

    Joined:
    Aug 24, 2023
    Posts:
    3
    Thanks I'll give it a try! Since you asked, what I'm trying to do is make it so my enemy will face the player via an accelerating angular velocity, so if the player is to the enemy's right it will turn clockwise and if its to the left it will turn counterclockwise. I know there are easier ways to make my enemy face the player but for my purposes I want to use angular velocity. What you said should work but by all means tell me if there's a better way to do this.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Is it for a spaceship or something like that which has turning momentum?

    One way to achieve this is to compare headings using Mathf.DeltaAngle()

    The two headings you would consider are (obviously) the heading of the enemy's nose compared to the heading (bearing) towards the player you want him to face.

    You can compute headings by using Mathf.Atan2() and Mathf.Rad2Deg

    Once you get the basics working it will likely "overshoot" and oscillate back and forth.

    Angular damping can help here, but for a correct solution you could implement a simple PD filter. Simply turning always towards it would be a form of a P filter.
     
    Megalythyx likes this.
  5. Megalythyx

    Megalythyx

    Joined:
    Aug 24, 2023
    Posts:
    3
    This worked perfectly! Here's what the code ended up being if anyone finds this:
    Code (CSharp):
    1.   void Update()
    2.     {
    3.         VectorToPlayer = Player.transform.position - transform.position;
    4.  
    5.         AngleToPlayer = Mathf.DeltaAngle(transform.localRotation.eulerAngles.z, Mathf.Atan2(VectorToPlayer.y, VectorToPlayer.x) * Mathf.Rad2Deg - 90);
    6.  
    7.     }
    "AngleToPlayer" is negative when the player is to the left, and it's positive when the player is to the right.
     
    Kurt-Dekker likes this.