So I am looking at the scripting tutorial where it describes the script for creating a simple FPS camera, and I decide to rotate the camera. This is my script
var speed = 5.0;
function Update()
{
// Keep track of rotation on the Y (up) axis
var rotateY = 0;
// As per the tutorial
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
// Use the Q key to rotate
if (Input.GetKey("q"))
{
rotateY = 1 * Time.deltaTime * speed;
print(rotateY); // always output zero!
}
transform.translate(x, 0, z);
transform.rotate(Vector3.up * rotateY)
}
For some reasons, this code does not work. Whenever I hold down the Q key, a value of zero is output. However, if I remove the var rotateY = 0 from the top and change the if block to look like this
if (Input.GetKey('q'))
{
var rotateY = 1 * Time.deltaTime * speed;
....
}
The code works. Why so? For the first script, when Update is invoked, it sets rotateY to zero. The Q key is held down, so rotateY is calculated, but yet it becomes zero. (I did try print (1 * Time.deltaTime * speed and the output is not zero).
Any suggestion?