Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

using GameObject.Forward to determine another object is in front, behind, below...

Discussion in 'Physics' started by rsud, Oct 31, 2019.

  1. rsud

    rsud

    Joined:
    Aug 13, 2010
    Posts:
    88
    Hello,
    I want to know if an another gameobject is behind, in front of, above, below, etc from my player gameobject.

    Whats the fastest (low overhead) way to do this?

    I was thinking to calc the yaw and pitch of the other gameobject.from my player gameobject so a yaw and pitch of 180 would be behind my player. yaw and pitch of 0 would be in front. A yaw of 90 and pitch of 0 might be right or left depending on direction the yaw value is increasing. A pitch of 90 and yaw of 0 would be above or below, again based on the direction the pitch.value is increasing.

    Is this a fast efficient way? If so, next question, of course....

    Given two game objects how can I get the yaw and pitch from one of the gameobjects using its forward vector?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,571
    Get the sign of the Vector3.Dot operation between the player object's forward / up / right vectors and the relative position of the other GameObject. Example assuming this script executes in your player GameObject:
    Code (CSharp):
    1. Vector3 relativePos = otherObject.transform.position - transform.position;
    2.  
    3. bool isInFront = Vector3.Dot(transform.forward, relativePos) > 0.0f;   // Otherwise is behind
    4. bool isAbove = Vector3.Dot(transform.up, relativePos) > 0.0f;    // Otherwise is below
    5. bool isAtTheRight = Vector3.Dot(transform.right, relativePos) > 0.0f;    // Otherwise is at the left
    This way you've divided the space in 8 quadrants relative to the player, and you can quickly figure out which quadrant any other object is in.
     
    Last edited: Oct 31, 2019
    rsud likes this.
  3. rsud

    rsud

    Joined:
    Aug 13, 2010
    Posts:
    88
    Thanks, converted your code as follows and this gets me 3/4 way (maybe) to yaw and pitch and gives me the position based on gameobject forward coordinate system.

    Vector3 localOffset = new Vector3(Vector3.Dot(transform.right, relativePos),
    Vector3.Dot(transform.up, relativePos) ,
    Vector3.Dot(transform.forward, relativePos));
     
  4. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    1,951
    You can make it faster though. When you write transform.right, what happens under the hood is transform.rotation * Vector3.right, so you currently have 3 Quaternion * operations and 3 completely useless Vector3.Dot operations.

    Code (CSharp):
    1. Vector3 localDir = Quaternion.Inverse(transform.rotation) * (otherObject.transform.position - transform.position);
    2.  
    3. bool isForward = localDir.z > 0;
    4. bool isUp = localDir.y > 0;
    5. bool isRight = localDir.x > 0;
     
    Edy and rsud like this.
  5. rsud

    rsud

    Joined:
    Aug 13, 2010
    Posts:
    88
    Thanks!
    I do want to calculate the yaw and pitch value based on the forward of one gameobject in relation to another, which was my ask intent.

    Is there a faster coding method to get a yaw and pitch based on the forward of a gameobject?
     
  6. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    1,951
    Code (CSharp):
    1. float yaw = Mathf.Atan2(localDir.x, localDir.z) * Mathf.Rad2Deg;
    2. float pitch = Vector3.Angle(Vector3.down, localDir) - 90f;
     
    Edy likes this.