- Home /
Really Confused about Scripting Rotations
Hello everyone. I am new to Unity and 3D game development and have been messing around with creating controls for an object(ie Spacecraft in 3D space). I have been making some great progress but I am really stuck on something. Here is what I am confused about.
Basically what I am am trying to do is get the object to rotate a certain number of degrees left or right the same way you would by changing the numbers in the inspector panel. When you change the numbers in the inspector panel the object's rotation changes(tilts). I would like to do the same thing in script when certain keys are pressed. No matter what I have used, it seems like the object always "orbits" a center rather than just changing its orientation like it does in the inspector panel. Can someone point me in the the right direction and tell me which function I need to use to get the same result in script that you do if manually changing the rotation numbers in Inspector panel. Thank you in advance for any help you can offer.
Unity uses quaternions to represent rotation. Have a look at the UnityGems explanation of Quaternions.
Answer by ChrisSch · Feb 27, 2014 at 09:42 PM
Yes rotation can be confusing, especially for beginners. I never really tried anything like it before but here's what I cooked up so fast. If you just want to snap to those rotations as if you're writing it in the inspector, I think what you're looking for is euler angles.
https://docs.unity3d.com/Documentation/ScriptReference/Quaternion-eulerAngles.html
Here's a quick code that will allow you to add for example 30 degrees rotation on Y axis every time you press left or right, so it rotates the object by 30 degrees to the left or right.
var rotationDegrees : int = 30;
function Update()
{
//Change GetKeyDown to GetButtonDown, I just used keys for simplicity's sake.
//But any controls you want to be changed by the player use buttons not keys.
if(Input.GetKeyDown("a")) //To the left
{
var leftRotation = transform.rotation.eulerAngles;
transform.rotation.eulerAngles = Vector3(0, -rotationDegrees, 0) + leftRotation;
}
else if(Input.GetKeyDown("d")) //To the right
{
var rightRotation = transform.rotation.eulerAngles;
transform.rotation.eulerAngles = Vector3(0, rotationDegrees, 0) + rightRotation;
}
}
If you wanna rotate on a different axis just put the "rotationDegrees" into one of the other axis in the "Vector3(x,y,z)" part.
But if you want to just rotate it to 30 or 170 or whatever exactly as if typing in inspector then this code should do the trick.
var rotationDegrees : int = 30;
function Update()
{
if(Input.GetKeyDown("r"))
{
transform.rotation.eulerAngles = Vector3(0, rotationDegrees, 0);
}
}
I probably confused you with leftRotation and rightRotation, it isn't actually left or right I might as well typed in rotation1 and rotation2, because it just gets your current rotation vector and adds the desired rotation vector in the next line.
Also Jamora gave you a great link! Definitely read that as eulers aren't really suited for more complicated rotations, you'll run into problems. So definitely take a look at that link.
Answer by DanielRS · Feb 27, 2014 at 10:44 PM
There's a chapter about this in Unity's project tutorials Space Shooter: 6. Moving the player. It explains how to "tilt" the ship to the left or right depending on player input.