Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Strange behaviour of rotating game object

Discussion in 'Scripting' started by herbie, Nov 25, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    In my android game I have a sphere which you can turn around (360 degrees) with your finger (up and down around the x-axis).
    At the front of the sphere there is a button. When you hit the button, the sphere will rotate to a fixed angle, for example 60 degrees up. Thats working fine.
    The code for this:
    Code (JavaScript):
    1. transform.rotation.x = Mathf.Lerp(transform.rotation.x, 0.5, rotateSpeed);
    But when I first turn the sphere around for 360 degrees and then push the button, the sphere will rotate 60 degrees down instead of up.
    What I would like to have is that the sphere always will rotate up, no matter if it is first turned around for 360 degrees.

    Does anybody know what's going on here?
     
  2. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I think it has something to do with the following.
    When you rotate the sphere upwards, Rotation X goes from 0 degrees to 90 degrees. Then further it goes from 90 degrees to 0 degrees instead of 180 degrees. Then it goes from 0 degrees to 270 degrees and then back to 0 degrees.

    So 0 - 90 - 0 - 270 - 0 instead of 0 - 90 -180 - 270 - 0

    I hope you understand what the problem is.
    What can I do about it?
     
  3. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    rotation is in Quaternions. You want to use eulerAngles.
     
  4. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237