Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    89
    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:
    2,497
    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:
    89
    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:
    2,548
    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;
     
    cloverme, Edy and rsud like this.
  5. rsud

    rsud

    Joined:
    Aug 13, 2010
    Posts:
    89
    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:
    2,548
    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.