Search Unity

Vector3 component values different than values from whole Vector3

Discussion in 'Editor & General Support' started by edwon, Jan 28, 2021.

  1. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    OK, in all my years of Unity dev, I've never seen something so f-ed up. Somebody help me dear lord.

    From this code:

    Code (CSharp):
    1.    
    2. void FixedUpdate()
    3. {
    4.         Vector3 noseVelocityLocal = noseRB.transform.InverseTransformDirection(noseRB.velocity);
    5.         Debug.Log(noseVelocityLocal + " y: " + noseVelocityLocal.y);
    6. }
    7.  
    Prints this in the console:
    (0.0, 0.0, 0.0) y: -5.67577E-07

    How in the world is this even possible
    the y component by itself is different than the y component in the vector3 as a whole, how?!!!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    The default
    .ToString()
    method for
    Vector3
    only displays one decimal place.

    The default
    .ToString()
    method for
    float
    shows you more precision

    In any case,
    float
    quantities should NEVER be compared for equality due to floating point imprecision.

    Google that up and you'll see why.

    Instead, compare for less than and/or greater than (crossing threshholds), or for being within a certain difference (such as with Mathf.Approximately())
     
    bobisgod234 and Vryken like this.
  3. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Yes I am aware of both of those things, but did you look at my code? I'm not doing an equality comparison, and the difference between the Vector3 output and the y component output is not just precision, it's whole numbers. Like what?!

    The reason I found this is because I'm doing some physics adjustments and I need to get just the Y, do some changes on it, and then apply it again, but once I do that the numbers are crazy.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Actually... that is a form of scientific notation called "E Notation":

    https://en.wikipedia.org/wiki/Scientific_notation

    -5.67577E-07 means -5.67577 times (10 raised to the -7 power)

    so -5.67577 divided by 10,000,000 ... that is a veeeeeeery tiny number, not at all a whole number.

    Or just shift the decimal place left 7 places, inserting zeros... I'm not gonna try and type it.
     
  5. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    I see ok I didn't realize that, me feel foolish, my physics bug must be originating elsewhere then