- Home /
Damping Rotation Reset
I am currently using this code to reset the rotation of my game object.
if(Input.GetKey(KeyCode.S)){
turretRotation.localRotation = Quaternion.Euler(Vector3(0,0,0));
}
That works fine and dandy, but it instantly resets the rotation. Is there a way I can dampen the reset?
Answer by robertbu · Sep 28, 2014 at 03:47 AM
This can be a tricky problem. You can do this:
if(Input.GetKey(KeyCode.S)){
turretRotation.localRotation = Quaternion.RotateTowards(turretRotation.localRotation, Quaternion.identity, speed * Time.deltaTime);
}
'speed' is a variable you define and is degrees per second. Note this code will only rotate as long as the 'S' key is held down, so it is not guaranteed to get back to (0,0,0). You can put the rotation in a coroutine, but you have to be careful to suspend other rotation on the object until the coroutine is complete.
var rotating = false;
var speed = 90.0;
if(!rotating && Input.GetKey(KeyCode.S)){
RotateHome();
}
function RotateHome() {
rotating = true;
while (turretRotation.localRotation != Quaternion.identity) {
turretRotation.localRotation = Quaternion.RotateTowards(turretRotation.localRotation, Quaternion.identity, speed * Time.deltaTime);
yield;
}
rotating = false;
}
Your answer
Follow this Question
Related Questions
Smooth rotation in 90° increments 0 Answers
new Array storing rotations 1 Answer
Need help converting Euler-based rotation to Quaternion-based (gimbal lock). 1 Answer
Euler angle problems with rotation limit script 3 Answers
Gun Rotation 2 Answers