Hey guys, I am just getting my feet wet in scripting so please bare with me. I’ve searched through the forums on this topic and from what I gather the solution likely involves converting to quaternions or vector3. Unfortunately, I don’t understand exactly where or how I handle this in my script. All I’m trying to do is simply tumble/rotate an object using the arrow keys, unfortunately as soon as I rotate on Y the gimbal axes are thrown off and the rotations no longer work as desired. Any help here would be greatly appreciated, and any explanation as to why and how the given solution works would be extra appreciated! Teach a man to fish
Here is my current (very basic) script- please help!
function Update () {
if (Input.GetKeyDown ("up")) {
transform.Rotate(90,0,0);
}
if (Input.GetKeyDown ("down")) {
transform.Rotate(-90,0,0);
}
if (Input.GetKeyDown ("left")) {
transform.Rotate(0,-90,0);
}
if (Input.GetKeyDown ("right")) {
transform.Rotate(0,90,0);
}
}
As written, your rotations are around the object’s local X and Y axes. You can pass ‘Space.World’ as an extra argument to Rotate() if you want to make it rotate around the world X and Y axes, and that might work better for you - assuming your camera is in the right place!
im kinda new to unity also, was able to reproduce your issue. once you pass +90 on 1 axis the other axis switch over to 180.
i notice this also happens when you manually change the number in the inspector or manualy pull the rotation line on the editor, once you reach +90 it switch the otehr 2 axis to 180 and start decreasing the +90 value.
not sure the exact logic, but im thinking its got something to do with a cube past 90 on on axis can be reproduced by fliping the other axis. i would also like to know if my thinking is correct and how we stop it from doing this and simply allow me to go the whole 360 without deciding that other axis should be rotated to compensate.
btw the object still looks ok, im wondering if this is all normal and we simply should never be refrencing rotation coord x y z and use the diffrent functions to atteint what we doing.
Just an idea, but do you ever think that putting you object inside of another, placing that inside another and finally p,acing that inside another, then rotating each, by a single, different axis. The result would be that the object you want to appear rotating does, but you simply manage it a bit easier by placing a single rotation to a single axis of each of its parents.
i place a cube ni editor . i change its x axis to say 110. it automatically changes it to 70 on x and adds 180 on y and 180 on z. it comes out to the same thing, but i think he wants to know ( and im curiouse) anyway to have it refrenc uptp 360 without auto wrapping the rest of the axis?
gfoot- That was exactly the solve I needed- thanks!
Thanks everyone for the replies.
renman- That’s a good thought- unfortunately, I don’t think it would work out. By creating a hierarchy of rotations like that you are basically building a gimbal rotation system, so it would suffer from the same problems. As soon as you rotate one of the higher nodes in the hierarchy the nodes below it would transform with it, so their axes would be shifted and no longer would line up in correct direction and they can end up overlapping and causing gimbal lock.