- Home /
Combining 2 Rotations: Global and Local
Here is the problem: ship on the sea (XY plane).Ship turns left right ok. When turning I want the ship to heel (rotate a little along the longitudinal axis).
Left right turning works ok with this code:
var rotationVector = GameObject.Find ("SHIP").transform.rotation.eulerAngles;
rotationVector.x = 0f;
rotationVector.y = 0f;
rotationVector.z = rotationVector.z + 0.5f; //well , a function anyway
GameObject.Find ("SHIP").transform.rotation = Quaternion.Euler(rotationVector);
If I try to add the second rotation on the Y axis (the heeling), the resultant rotation gets weird.
var rotationVector = GameObject.Find ("SHIP").transform.rotation.eulerAngles;
rotationVector.x = 0f;
rotationVector.y = rotationVector.y+ 0.5f;
rotationVector.z = rotationVector.z + 0.5f;
GameObject.Find ("SHIP").transform.rotation = Quaternion.Euler(rotationVector);
As far as I get it, the rotation is local, and heeling takes steering off the horizontal and the ship goes either airplane or submarine. I could make an Z adjustment each frame to compensate but I don't get the heeling! Tried 2 rotations stages, tried fist one then the other, variants from forums but I can't get it working..
How to get the banking local and the turning global on the same object?
This is getting me bugnuts Heelp!?!
transform.localRotation to set the local rotation. But why is the rotation local? Is the ship a child of something?
Hello, The ship is not a child of anything. Excluding the blend model, it's a new rectangle that I want to move like in this photo
I am attaching a txt file that you can paste into a script to illustrate my problem. Just attach it to the camera.link text
If I'm doing it backwards (as I'm probably doing) please someone send me the code for doing it right!!
Thanks!
Answer by Paulius-Liekis · Feb 22, 2015 at 10:07 AM
It might be that the order of rotations is different than you expect. Try this (of flip the order to y/z quaternions):
GameObject.Find ("SHIP").transform.rotation =
Quaternion.Euler(new Vector3(0, rotY, 0)) *
Quaternion.Euler(new Vector3(0, 0, rotZ));
Answer by Daniel Greenhorn · Feb 22, 2015 at 04:14 PM
It turns out the solution was
GameObject.Find ("SHIP5").transform.rotation = Quaternion.AngleAxis(rudder5, Vector3.forward)*Quaternion.AngleAxis(heel5, Vector3.up);
Well, the angle changes from 0 to 360 and from 360 to 0 when passing 0 but that can be dealt with with a ton of IF's for both rudder and heel angles.
For anyone having this problem, I'm attaching the whole code