float speed = 20;
Rigidbody2D rb2D;
rb2D = GetComponent();
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.
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.
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.
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.