Search Unity

absurd z value from otherwise ordinary-looking (and tested) Vector3

Discussion in 'Scripting' started by mrdatsun, May 7, 2020.

  1. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    I can't fathom this one out. I'm getting z values in my code for two different GameObjects, using position Vector3s to store the information. One of the z values always wrong - a negative exponential. I have examined the x and y and they print normally. Any explanation, please?

    Code (CSharp):
    1. //declaration
    2. Transform leftFoot;
    3. Vector3 startStrideLoc;
    4.  
    5. //instantiation
    6. leftFoot = GameObject.Find("leftFoot").transform;
    7.  
    8. //later, set variable
    9. startStrideLoc = leftFoot.position;
    10. ----
    11. //access and print the values - one statement directly after the next in my code
    12.  
    13. print("dataL:"  + startStrideLoc);
    14. //prints - dataL:(-1.0, 0.2, 0.0) -- expected value
    15.  
    16. print("dataL:"  + startStrideLoc.z);
    17. //prints - dataL:-2.33232E-07 -- where does this come from?
    Full class file linked.
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's just a rounding error. That number typed out is about -0.0000002. For all intents and purposes, it's zero, which seems to match with your expectation. I bet if you print your x and z to more decimal places you might find they are not "exactly" what you expect either:

    Code (CSharp):
    1. print($"dataL: {startStrideLoc.ToString("F8")}");
     
  3. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    26
    Thank-you. I think I've never got used to reading exponential number syntax, and when I had a bug in that side of the code, I irrationally blamed it on the look of that number. I'm working through to find the true cause now.

    I'll mark this thread as closed, but it could easily be deleted I guess.