- Home /
How Time.deltaTime affects movement ?
float speed = 20;
Rigidbody2D rb2D;
rb2D = GetComponent<Rigidbody2D>();
Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed, rb2D.velocity.y);
rb2D.velocity = moveDir;
This a simple code to move horizontally(it works) but I heard that it is very important to use Time.deltaTime in order to move by time and not by the speed of your CPU however when I put like this: Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, rb2D.velocity.y);
Since Time.delta Time gives me a number less than 0 and logically he moves so much slower almost nothing. SO as I said how do I implent the Time.deltaTime if it's necessary of course.
Thanks for reading.
Thanks for your reply. To resolve the problem I just increased speed (from 20 to 1500) and now runs fine I guess put:
speed = 1500//or any increased speed;
Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, rb2D.velocity.y);
Yet, I think it's a bit strange. Never saw a person putting such high value just to move but oh well...
I noticed another problem, even with increased speed, when I turn off vsync, the game has more FPS but the character runs so slow. It shouldn't happening since this method should be working by time and not FPS.
Answer by tanoshimi · Aug 18, 2016 at 07:31 PM
If you want framerate-independent kinematic movement (i.e. you're moving the object, via Translate() or explicitly setting the value of transform.position) then multiplying by Time.deltaTime is essential.
But in this example, you're not moving the object; you're setting the rigidbody's velocity and letting the Physx engine determine how far the object should move in each frame accordingly. It doesn't make any difference if you set a velocity of 20 once a second or 50 times a second - it's still going to be 20 in every single frame when the physics calculation is performed, and that's why you've not noticed a difference in this case.
Answer by ZammyIsOnFire1 · Aug 18, 2016 at 05:04 PM
Your speed modifier should be used to modify your speed. Use it as a per second movement speed modifier.
So if you want to move 1 unit per second you can leave it at 1. If you want to move 20 units per second leave at 20.
Fast and slow depends on your camera settings and how much is 1 unit in the game.
Answer by DREZZ3R · Aug 18, 2016 at 07:19 PM
Well guys, I discovered that for this movement method you don't need Time.deltaTime. I did a test with a timer. The test involved the player to reach a certain location in a straight path and when he reaches the goal, it shows in the console how much time I spent reaching the goal and this was the results;
3.403seg - locked at 60fps
3.400seg - unlocked(2800fps+)
3.424 - vsync
In conclusion, this script for moving(the very first one I posted)doen't need Time.delaTime. It's just a dfference of MilliMilliSeconds.
@meat5000 commented the answer with a video.
Your answer
Follow this Question
Related Questions
Quake Movement in Unity 1 Answer
isKinematic does not always work 0 Answers
My player's movement is slippery 1 Answer
Jump only sometimes works and player doesn't always jump high? 1 Answer
Non slippery movement 1 Answer