Chapter 7. 3D 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.
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.
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.
7.1 Running Code a Specific Number of Times per Second
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access