- Home /
Rotating an object on z axis 180degrees C#.
I can't seem to figure out why this code is sometimes working and other times not. Sometimes when i press the key to rotate the object it works, and sometimes it doesn't. I just need an object to rotate 180 degrees on z axis. Any help would be appreciated. code :
if (Input.GetKey(KeyCode.Keypad0)) {
targetRotation = Quaternion.LookRotation(transform.forward, Vector3.right);
transform.rotation = Quaternion.Slerp(targetRotation,transform.rotation, smooth * Time.deltaTime);
}
Answer by robertbu · Mar 11, 2014 at 10:39 PM
Here is a bit of code that will cause an object to rotate on its local 'z' axis on the push of a key:
#pragma strict
var targetRotation : Quaternion;
var smooth : float = 3.0;
function Start() {
targetRotation = transform.rotation;
}
function Update() {
if (Input.GetKeyDown(KeyCode.Keypad0)) {
targetRotation = Quaternion.AngleAxis(180.0, transform.forward) * transform.rotation;
}
transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation, smooth * Time.deltaTime);
}
I just noticed you asked for C#. I guess you got it translated.
Your answer

Follow this Question
Related Questions
Rotate a 3D model left or right only 1 Answer
How to rotate the Rigidbody based on the normal of the ground?? 1 Answer
How to align the transform.rotation.y with the world space vector y? 1 Answer
Make object lay flat on a sphere + look up? 0 Answers
Issue when freezing Quaternion rotation in one axis? 1 Answer