Search Unity

is transform rotation is bugged?

Discussion in 'Scripting' started by vargata, Jan 19, 2014.

  1. vargata


    Nov 26, 2013
    Hi all

    I have a little problem with creating a spaceship control. Not because I dont know how to do it, but because things doesn't work as they supposed to work.
    here are 2 little videos: and
    the first is with rotating around world Y. it's a lot better until I pitch up, the second one shows the intended working mechanism but on that at 0:25 you can see that if I start circling with the mouse, the object start to rotate around Z, and thats what I don't know where it comes from.
    here the code is:

    As you can see I was experiencing with a few methods, the transform.Rotate was the best (second video) but still far from perfect.
    Last edited: Jan 19, 2014
  2. TheShane


    May 26, 2013
    Accumulated x- and y-axis rotations like that will create rotations on the z-axis as well eventually. There will also be accumulated floating point error when you are applying small relative rotations every frame.

    If transform.Rotate() works basically how you want, then use that. Correct the error with Quaternion.FromToRotation(transform.up, intendedRollAmount). Or use Quaternion.Slerp to make it smoother, but if you are correctly it constantly you will never get huge amounts of error piling up so both methods should end up looking similar.

    I would also use Quaternion.AngleAxis to create the localRotation and assign it to transform over using transform.Rotate. It gives you a bit more direct control over exactly what's happening.