Search Unity

Float_x - Float_x != 0? Look rotation viewing vector is zero - Refuses to return 0

Discussion in 'Scripting' started by BadSeedProductions, Aug 17, 2017.

  1. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    *Partially UNSOLVED

    I'm not entirely sure how to ask this question, as I have no idea whats going on here but I'm going to do my best to explain, hopefully someone can chime in and inform me whats happening here!

    Problem: Float_x - Float_x != 0

    I have 2 float values. One float is declared in the beginning of the script like so:

    Code (CSharp):
    1. float float_x = -127f;
    The other float is taken from an instance of another script, and this seems to directly correlate with the issue. We'll call this float_y, and float_y is a y value from a Vector2.

    IF the float is the same, I get an incorrect result and this message in the console: Look rotation viewing vector is zero (this is just a message not an error or warning) which is strange because I'm not attempting to rotate anything...

    example output: float_x - float_y = -7.629395E-06.

    It cannot simply be float precision issues, I am not even using any decimal places (same result when I do though).

    if float_y is set to any value other than the same value as float_x, it returns the proper result and no error. It simply does not want to equal 0.

    Any ideas?
     
    Last edited: Aug 17, 2017
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    You can't compare floats to exact value since they can't be precise. There's always a small offset on them. You should Use Approximately()
     
    BadSeedProductions likes this.
  3. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    Ok!so would this be the best way get the difference?:

    Code (CSharp):
    1. if (Mathf.Approximately(float_x, float_y)){
    2. float difference = 0f;
    3. }
    4. else{
    5. float difference = float_x - float_y;
    6. }
    7.            
    edit: still receiving this Look rotation viewing vector is zero message...edit :I found that I get this message when I move the cursor off of the console AND the app is not playing in the editor.

     
    Last edited: Aug 17, 2017
  4. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Yeah that seems to be correct.

    The Look rotation viewing vector is zero means pops when you try to call a rotation method towards a vector zero (0,0,0) so you will have to investigate where this might happen. What are you using your difference for?
     
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    So without seeing your code....

    The problem is basically kinda like a divide by 0 error... that results in an infinite number of possibilities. In this case you're trying to set your look rotation towards a vector with no length, which an infinite number of rotations would satisfy..so this error is thrown because Unity is basically saying "we don't think that we should guess what you want here". So the solution is to have your own default look rotation (previous rotation, perhaps) for when the value you're supplying to your LookRotation function is Vector3.zero

    example...
    Code (csharp):
    1. if (yourVector3 != Vector3.zero)
    2. {
    3.  // do lookrotation with yourVector3 as the param
    4. }
    5. else
    6. {
    7.   // do whatever you want it to do instead
    8. }
    Edit: @Rotary-Heart .. sorry, guess you snuck a reply in just before mine
     
  6. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    I am using the difference to plot a point in x,y coordinates using a Vector2. Nothing to do with rotation... (in fact I searched my scripts for any reference to quaternions and rotations, there is nothing in the entire app that is being rotated.) I edited my above post to include a video of the console. It's very strange behavior. I found that I get this message when I move the cursor off of the console AND the app is not playing in the editor.

    However, the code now works correctly, Im just trying to figure out what this deal is with Unity spamming the console...
     
    Last edited: Aug 17, 2017
  7. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    That's odd do you have something looking at your mouse? It seems to be related to the mouse coordinates so I would check where in your code you are using the mouse coords