In the following code, when you press the WASD keys the object rotates in that direction. On releasing the keys the object should rotate back to its initial rotation, (which is 0,0,0).
This works, but the resetTimer variable should control the time taken to rotate back to the start position. However, the lower the value of resetTimer, the longer it takes. This seems counter-intuitive. Surely the higher the value, the longer it would take. Can anyone explain why this is the case?
The code of interest is at the bottom, I’ve included the whole lot so people can see what I’m trying to do.
var rotSpeed : float = 100.0;
var durationPressed : float = 0.0;
var resetTimer : float = 6;
var keyPressed : boolean = false;
var startRot : Quaternion;
//var timeVar : float = Time.deltaTime * resetTimer;
function Start ()
{
startRot = transform.rotation;
}
function Update ()
{
// w key down
if (Input.GetKey ("w"))
{
transform.Rotate(rotSpeed * Time.deltaTime, 0, 0 );
durationPressed += Time.deltaTime;
keyPressed = true;
}
// w key up
if (Input.GetKeyUp ("w"))
{
durationPressed = 0;
keyPressed = false;
}
// a key down
if (Input.GetKey ("a"))
{
transform.Rotate(0,0, rotSpeed * Time.deltaTime );
durationPressed += Time.deltaTime;
keyPressed = true;
}
// a key up
if (Input.GetKeyUp ("a"))
{
durationPressed = 0;
keyPressed = false;
}
// s key down
if (Input.GetKey ("s"))
{
transform.Rotate(-rotSpeed * Time.deltaTime, 0, 0 );
durationPressed += Time.deltaTime;
keyPressed = true;
}
// s key up
if (Input.GetKeyUp ("s"))
{
durationPressed = 0;
keyPressed = false;
}
// d key down
if (Input.GetKey ("d"))
{
transform.Rotate(0,0, -rotSpeed * Time.deltaTime );
durationPressed += Time.deltaTime;
keyPressed = true;
}
// d key up
if (Input.GetKeyUp ("d"))
{
durationPressed = 0;
keyPressed = false;
}
if (!keyPressed)
{
transform.rotation = Quaternion.Slerp(transform.rotation, startRot, Time.deltaTime * resetTimer);
}
}