I’ve been working on a Unity Project “Space Shooter” (http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player) and I slightly modified one of the scripts that deals with player movement (and in my case how the player’s ship banks according to player speed) and I have included the relevant code at the end of the post.
I’ve added time.deltaTime as a multiplier into the line:
rigidbody.velocity = playerMovement * speedModifier * Time.deltaTime;
Which is then fed into:
rigidbody.rotation = Quaternion.Euler (0.0f,0.0f,rigidbody.velocity.x * -tilt)
Now, without time.deltaTime, the Euler math works out exactly as expected (say player movement was 1, speedModifier was 2, and tilt was 5: you would get a max rotation of (1*2) * 5 = 10 degrees.
The problem is that when time.deltaTime is active, I don’t understand how the max player rotation is derived (and in this case when applied to small numbers, the player ship barley even moves or rotates at all). Using values that equaled what would normally be 500 degrees with time.deltaTime, I end up with a 10 degree max rotation…how?
Now, of course you could figure that 500 * the value of time.deltaTime must = 10, but I thought that time.deltaTime varies from frame to frame (and I doubt every frame even in a simple game renders at the exact same speed), so there should be some variance each time I turn (say 10, 10.2, 10.05, etc). But every time I turn, I get 10, and I don’t know why. Any ideas?
public float speedModifier = 100.0f;
public float tilt = 5.0f;
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 playerMovement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = playerMovement * speedModifier * Time.deltaTime;
rigidbody.rotation = Quaternion.Euler (0.0f,0.0f,rigidbody.velocity.x * -tilt);
}