Join Michael House for an in-depth discussion in this video Updates and Delta Time, part of Advanced Unity: 3D Game Programming.
In this video I'm going to tell you about Delta time and the update loop in Unity. The update loop is a fundamental part of modern games. All the code in the update loop runs every frame. Frames are just like frames in a film. They're static images that when displayed one after another, create the appearance of smooth movement. In this sense, we can think of our game as a film, where each frame is set up and rendered right before its displayed. The Delta in Delta time comes from the Greek letter Delta. Which is used in Mathematics to mean finite difference.
So Delta time is the amount of time that's passed since the last frame, or the difference in time between two frames. Delta time is important for a number of reasons, for example, in the image we see here. We see three frames of a sphere moving up. This sphere has a speed and that speed is distance over time. But Delta time allows us to calculate the distance that sphere should move from one frame to the next. In our earlier script we simply added a small amount to the position, to move the object. This is actually a bad practice and can cause problems.
For example, consider two identical games, except one is running at 120 frames per second and the other is running at 30 frames per second. If we let each game run for one second, the object in the game running at thirty frames per second, is only going to move a quarter of the distance, of the object in the faster running game. Using delta time, we can give our game more consistency, regardless of the speed it runs. We call this frame rate independence. In Unity, Delta time is measured in seconds. This means that when choosing a speed, it will be X units per second.
The unit in this case will vary from game to game. We could use inches, feet, yards, whatever we like. By default, you need to use as meters. An example of this would be if an object's speed was three after one second, the object would have moved three meters. In order to move n meters per second, we're going to scale the speed by Delta time. Imagine that if Delta time was one second. That would mean our distance traveled would be equal to speed times one. This is great since we want our speed to be in meters per second.
Of course Delta time's typically much smaller than one second, so the distance moved should be fairly small. Let's see this in practice and update our code. So simply removing the constant amount here and adding a small amount, so 3 times Time.deltaTime. Now our object will move up and 3 meters per second no matter the frame rate. Let's see this in practice. We'll save and switch back to Unity. There it goes. Up out of the screen at 3 meters per second. In this video showing you what Delta time is, why it's important and how to use it to produce more consistent results.
We also looked at high level view of behind the scenes workings of Unity in the update loop. Using what we know about the update loop and Delta time, we can achieve frame rate independence in our game. Frame rate independence is important because it provides a deterministic experience regardless of the speed the game runs.
NOTE: This course requires Unity 4.5.5. The newer versions of Unity have done away with the GUI system used in this course, so the interfaces included for many of the scenes will not work with 4.6 and higher. You can download Unity 4.5.5 at http://unity3d.com/get-unity/download/archive.
- Enabling/disabling with scripts
- Translating, rotating, and scaling objects with scripts
- Working with mouse and keyboard input
- Creating custom GUI controls like progress bars
- Loading assets with scripts
- Saving games
- Creating prefabs dynamically
- Making remote procedure calls
- Synchronizing object transforms
- Finding slow code
- Optimizing data access
- Extending the editor
Skill Level Advanced
Q: In the Chapter 3 file EntityLoader.cs, I get an error: DirectoryNotFoundException: Directory 'Assets\' not found. Removing the backslash after 'Assets' fixes it, but then I run into a different error. How do I fix this?
Q: What version of Unity does this course cover?
A: This course requires Unity 4.5.5. The newer versions of Unity have done away with the GUI system used in this course, so the interfaces included for many of the scenes will not work with 4.6 and higher. You can download Unity 4.5.5 at http://unity3d.com/get-unity/download/archive.
Q: The game object in the first chapter's Mouse Input Raycasting sample doesn't appear to follow the mouse. What's wrong?
A: Please enable the Box Collider component (by activating the checkbox next to the component name) on the game object. This is not explicitly mentioned in the video, but it will ensure the raycasts collide with the game object.