Search Unity

Simple IF test not working..

Discussion in 'Scripting' started by Fillock, Oct 3, 2015.

  1. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Hi!

    This was ment to be the simplest test of all time:

    Code (JavaScript):
    1. var test = vehicleTransform.localEulerAngles.z;
    2.         if (test <= 50) {
    3.             print ("CarClimbing");
    4.         }
    but if I code; "test <= 50" or "test >= 50" CarClimbing is still printed, what am I doing wrong?

    Thanks for any ansvers

    Fillock
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you might be running afoul of the fact that a quaternion converted into a euler representation could be many values, i.e. 0, 360, -360 are all the same angle.
     
  3. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    Hullo!
    Probably means that test == 50.
    The operators <= and >= mean "less than or equal to" and "greater than or equal to," respectively. The only way both statements would evaluate to true would be if test is, well, equal to 50. I would suggest printing the actual test value or exposing it via the inspector or something to check what the actual value is.
     
  4. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Hm, it can't be ==50, the gameobject I try to meshure is a car, and when I use this print in Update:
    Code (JavaScript):
    1. print ("vehicleTransform.localEulerAngles.z");
    it shows a lot of different values when driving up and down hills.... if it prints right, why dosent it test right?
    How do I run afoul?

    F.
     
  5. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Ai sorry, I print without the ""
     
  6. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    There must be an overlooked detail derailing expectations then...

    You are assigning vehicleTransform.localEulerAngles.z to a var test and testing that, but you say you are printing vehicleTransform.localEulerAngles.z (which is printing different values) but not printing test. Drilling down to the core issue would likely be alot easier if you posted the entire script.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It's most likely the issue that @LeftyRighty has already pointed out.

    @Fillock
    While not in playmode, the rotation shown in the editor can exceed the range of 0-360 into both directions, negative and positive.
    However, during playmode/runtime, the values will be kept in the range of 0-360 (360 exclusiv of course), probably due to floating imprecision, otherwise you'd run into some ugly problems when an object keeps rotating all the time and reaches much higher values.

    If your car happens to drive down (or up, that depends on the setup) a hill, the or orientation would theoretically be a negative number, but remember, Unity keeps it in a certain range. Imagine you've got a z-orientation of -50 degrees, Unity will make 310 out of it and the code you're using won't work as expected. This also applies for values >= 360.
     
  8. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Shure I post the hole script. Here I diged out the code from the carscript:

    Code (JavaScript):
    1. #
    2.  
    3. #pragma strict
    4.  
    5. var vehicleTransform : Transform;
    6.  
    7. function Update () {
    8.  
    9.   var test = vehicleTransform.localEulerAngles.x;
    10.   // print (test);  // in runtime this works, 360 the car on flat ground
    11.    
    12.   if (test >= 310) {
    13.   TireSlidingDown();
    14.   }
    15.   if (test <= 50) {
    16.   TireSlidingUpp();
    17.   }
    18. }
    19.  
    20. function TireSlidingDown() {
    21.   print ("Driving Upphill");  
    22. }
    23.  
    24. function TireSlidingUpp() {
    25.   print ("Driving Downhill");
    26. }
    27.  
    It work some kind of way, driving upphill it print "Driving Upphill"l and driving downhill it print "Driving Downhill". But on flat land its allways printing, it never stop.
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you haven't flipped anything around, flat land means ~0 +/- something. That's always covered by the conditions, if it's 0 or greater the second one will trigger, if it's therotical below 0 it's gonna turn into 3xx and the first one will match.

    You could add another condition to each as a threshold, so that you define a range of 310-340 and 20-50 for example.
     
  10. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Like this you mean:

    if (test >= 300 && test <= 0) {

    I tryed, it doesn't work, now it doesn't print anything at all...
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    No, like the example that i had given. The one that you posted cannot work because a number can never be greater than 300 and smaller than 0 at the same time.
     
  12. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    eee that's true.

    Code (JavaScript):
    1.     if (test >= 310 && test <= 340) {
    2.         TireSlidingDown();
    3.     }
    4.    
    5.     if (test >= 10 && test <= 40) {
    6.         TireSlidingUpp();
    7.     }
    Jep you are right, it works, thanks! :)