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?
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;
}