var upKey: KeyCode;
var downKey: KeyCode;
var leftKey: KeyCode;
var rightKey: KeyCode;
var gO: GameObject;
function Update () {
if(Input.GetKeyDown(upKey)){
gO.transform.rotation.z = 270;
}
else if(Input.GetKeyDown(downKey)){
gO.transform.rotation.z = 90;
}
else if(Input.GetKeyDown(leftKey)){
gO.transform.rotation.z = 0;
}
else if(Input.GetKeyDown(rightKey)){
gO.transform.rotation.z = 180;
}
}
When ever i go down or up it sticks at 180 degrees on the z axis. Can someone help me?
Tranform.rotation
is of type Quaternion. It’s w,x,y,z properties are not the angles you are expecting.
If you want to apply angles use the following pattern:
var angles: Vector3 = tranform.rotation.eulerAngles;
angle.z = 90.0f;
tranform.rotation = Quaternion.Euler(angles);
You are trying to modify the quaternions which I think is not what you intend. Quaternions have 4 components w,x,y,z. What you want to modify is the x, y and z rotation of the object. So, here is the script that should suit your needs:
var upKey: KeyCode;
var downKey: KeyCode;
var leftKey: KeyCode;
var rightKey: KeyCode;
var gO: GameObject;
function Update () {
if(Input.GetKeyDown(upKey)){
gO.transform.rotation.eulerAngles.z = 270;
}
else if(Input.GetKeyDown(downKey)){
gO.transform.rotation.eulerAngles.z = 90;
}
else if(Input.GetKeyDown(leftKey)){
gO.transform.rotation.eulerAngles.z = 0;
}
else if(Input.GetKeyDown(rightKey)){
gO.transform.rotation.eulerAngles.z = 180;
}
}