Chapter 7. Physics and Character Control
Physics is a crucial component of many video games. Whether your game is 2D or 3D, or something in between, you’ll be relying on some manner of physical force simulation at some point.
Note
Unity has two physics systems: one designed for 2D physics, which we discuss in Chapter 5, and one designed for 3D physics. This chapter is about the 3D physics system.
One of the most interesting things about physical simulation is that you get quite a bit of interesting gameplay opportunities for free. Human beings exist in a physical, three-dimensional world, and it’s very easy for a player to relate to your game when the game has “mechanics” that they interact with every day.
To that end, we’ll spend this chapter looking at ways to build gameplay systems that allow for character control in a physically simulated 3D environment, with a particular focus on first-person controls.
7.1 Understanding FixedUpdate
Problem
You want to write code that runs a fixed number of times per second.
Solution
Put the code in the FixedUpdate
method of your scripts.
Discussion
In your scripts, the Update
method is called every frame, which makes it ideal for almost everything that needs to be updated regularly.
Update
isn’t called at a regular pace; it depends on how long it takes to render each frame. If the scene gets more complex, the number of Update
calls per second will change.
By comparison, FixedUpdate
is called a fixed number of times per second. This ...
Get Unity Game Development Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.