Try reloading this page, or reviewing your browser settings
In this video segment, learn to move object on forward vector by a consistent speed using Time.deltaTime.
- Movement consistency
- Update function
About this video
- Alan Thorn
- First online
- 12 January 2019
- Online ISBN
- Copyright information
- © Alan Thorn 2019
[Audio Begins] [0:00:00]
Alan Thorn: In this movie we going to address a very common problem that manifests itself whenever we want to make objects move inside Unity. In particular, there is a problem of consistency. So in this movie we’re going to look at 1) what the problem is, that is the problem of movement consistency; and 2) how we can solve it by using the variable time dot delta time.
Here inside this scene here, we’ve created this car object which can move forwards at a particular speed. When I select the car object here, you can see that right now by default the speed is set to zero. But I’m going to change this to something like 0.01. When I press the Play button on the toolbar, the car will gradually move forwards along the driveway here, following the forward vector. Now right now, the car is moving pretty slowly. I’m just going to cancel that here. But the car is moving pretty slowly as it moves along the driveway. But of course, we could put this onto a different computer, and we might get slightly different results. Let me tell you why. I’m going to go back to visual studio here to take a look at our code. Inside the update function, remember the update function happens once on every frame. Inside the update function, we’re taking our position variable, and we’re updating that with additional values. In particular, I’m taking the forward direction, and I’m multiplying that by our speed. So effectively, I’m taking the forward arrow, and I’m stretching that out by our speed, and I’m adding that distance onto our current position in order to move the object forwards in the direction which it’s looking. Our speed here is defined at 1 meter per second.
But if we take a look at the update function, that value 1 meter per second is not in fact being applied. The reason for that is because the update function can happen many times per second. Maybe 100 times a second, maybe 70 times a second. In fact, across a single play session, even on the same computer, the frequency of the update function can change from second to second, depending on what the computer is doing. In one second, the update may happen 70 times. In a different second, it may happen 75 times. And if we take that onto a different computer, the problem only gets worse. Perhaps we take it onto a weaker computer which can only maintain a frame rate of 60 frames per second, meaning the update simply happens 60 times a second. Because of this variation in the number of times that update can be called, what is happening here is we’re dealing with the absolute speed value of 1, meaning that every single frame, this amount is being added onto the position of the object. The result is that this amount that we add onto the position of the object is going to vary over time, so that in one second the object might move forwards more than in a completely different second. What we want to do is we want to add some consistency to the speed so that the vehicle always moves forward consistently at a speed that we know about.
In this case I’m defining the speed as simply being 1 meter per second, that is for every single second, the car should move forwards by a meter no more and no less. No thankfully, Unity allows us to easily solve this problem by simply multiplying by Time.deltaTime. So anytime we need to move an object forward consistently, we simply need to multiply by Time.deltaTime. Now let me explain what that variable is by how it works. Time.deltaTime is a variable that Unity maintains internally. It simply is a fractional floating point value, and it tells us how much time in seconds has passed since the previous update. So update will happen, and then update will happen again, and Time.deltaTime tells us the amount of time between the past two updates. So for example, if Time.deltaTime is one, it means that one second has passed or one second has elapsed since the last time update happened. If Time.deltaTime were two it would mean that two seconds had passed since the previous update happened. Now typically, the values are not going to be that large. Instead, Time.deltaTime is going to be a fractional value like 0.5 or 0.025. It’s going to be a fractional value. This means that when we take our forward direction here and we multiply that by our speed and then we multiply that by Time.deltaTime, the resultant vector is scaled by this amount. So if Time.deltaTime were 0.5, then the forward vector multiplied by the speed would be halved to ensure that on each and every update, the car always moves forwards at a consistent speed, that is this speed here, which is measured as meters per second. So if ever you want to create consistency in motion, be sure to multiply your values by Time.deltaTime.
I’m going to use Ctrl-S to save this code and go back to Unity. It’s very likely that having made this change, I’ll have to go back to my script file and adjust the speed because now I want to effectively move forwards by 1 meter per second. By pressing Play here, the car is going to move forwards, and now it’s moving forwards by 1 meter per second. And it doesn’t matter how long I’m allowed to do this for, and it doesn’t matter how many different computers we play this on. Our objects are always going to move forwards by a consistent speed.
In this movie, we covered how to move objects forwards along their forward vector by a consistent speed. We achieved this by multiplying our displacement by Time.deltaTime. Time.deltaTime is simply a fractional value telling us how much time in seconds has passed since the previous update function.
[Audio Ends] [0:06:29]