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

Vector3 in if statement

Discussion in 'Scripting' started by kittik, Dec 13, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    How do I use a Vector3 in an if statement correctly?

    I am trying to determine if something is rotated towards a different object, but only on the X and Z rotation. I do not want to look for what the Y rotation is.

    Originally I tried -

    Code (CSharp):
    1. if(transform.eulerAngles == Vector3 (rotateToPosition.eulerAngles.x, y, rotateToPosition.eulerAngles.z))
    And it does not work. I know that conventionally a Vector3 looks for something more like (0, 180, 45), but as I am trying to find the Vector, I don't know how to go about writing it. I also do not know how to write that I don't need the Y Vector.

    Any help would be appreciated.
     
  2. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Still having problems, but it seems that I am looking at this wrong.

    I need to be checking if the object is looking towards the x and z positions of another object. I will have to somehow convert the positions to Vectors.

    Code (CSharp):
    1. if(transform.eulerAngles == Vector3 (rotateToPosition.transform.position.x, rotateToPosition.transform.position.y, rotateToPosition.transform.position.z))
    Is not valid.
     
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Code (CSharp):
    1.         if(transform.rotation.eulerAngles.x == rotateToPosition.transform.rotation.x
    2. && transform.rotation.eulerAngles.z == rotateToPosition.transform.rotation.z)
    3.  
    Is the above valid? No errors are produced by this code, but nothing is being called (I revamped the script earlier, so another issue may be causing nothing being called).
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You don't use vectors in equality statements.

    Vectors are a structure of 2,3, or 4 floats. Floats have an issue with something called 'float error'. This means that even though 2 values may appear to be equal, they're not necessarily equal. It's just the way floats are designed.

    Example, you might think 0.1 * 10 equals 1, but it doesn't... it usually comes out as something like 0.99999999999999, or 1.0000000000001. It's because 0.1 in binary is a repeating value, so it gets shaved down.

    It's like how in decimal, 1/3 * 3 = 1, but if stored in decimal 0.33333 * 3 = 0.99999

    So do NOT compare vectors through equality comparison.




    Furthermore, you're comparing euler angles. One euler rotation could be exactly the same as another euler rotation, and have completely different values. Because euler is not a good representation of rotation.
     
    kittik likes this.
  5. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    You should very rarely compare to vectors to each other directly with ==, on top of that, the eulerAngles will most likely never be equal to the position of another object. Instead, we can calculate the angle between two vectors to figure out if we're facing towards another gameobject.. Hope you were awake during your geometry and vector calculus courses! ...just kidding, Unity can do it for us. :)

    Example:

    Code (csharp):
    1.  
    2. void Update()
    3.  {
    4.  var other = GameObject.Find("Cube 2").transform.position; // just for my test
    5.  var facing = transform.right; // what direction your object is "facing" is not always clear cut
    6.  var self = transform.position;
    7.  
    8.  Debug.DrawLine(self, self+facing*100f, Color.green); //here we scale the facing vector by some amount to make it better visible
    9.  Debug.DrawLine(self, other, Color.red);
    10.  
    11.  // the vector from A to B is equal to B-A
    12.  // when calculating angles, we don't care about the magnitude of the vectors, just the direction
    13.  
    14.  var angleBetween = Vector3.Angle(facing, other-self);
    15.  if (angleBetween < 20f)
    16.  {
    17.  Debug.Log("Pretty close! Angle: " + angleBetween);
    18.  }
    19.  }
    20.  
     
    Munchy2007 and JoeStrout like this.