One of the most interesting aspects of Unity is it’s robust physics system that sits under the hood which allows developers to create some of the most compelling and interesting simulations in engine. This article will cover the basic aspects of the physics system giving you a quick introduction.
Hit Me: Colliders
The basic building block of the Unity physics system is the collider. There are about 5 different types of colliders those are Box, Sphere, Capsule, Mesh, and Terrain. These are listed in order from least to most expensive. This tells Unity when two Game Objects intersect with one another. Many of the prefabbed default Unity Game Objects come with these attached already.
Within colliders are Triggers, this disables the physics component of the collider and allows you use them as a zone another object can pass through. Which then allow events to be called when entering or exiting a Trigger. One of the objects must have a Rigidbody component in order for the Trigger to be detected and generally the moving object has the Rigidbody.
Stiffen Up: The Rigidbody
The Rigidbody Component allows Game Objects to interact with the physics engine enabling such properties such as mass, angular drag, and drag. You can explore these terms in the Unity Manual (here) for deeper explanations. But these properties create the fundamental effects different objects have when interacting with one another.
Use Gravity property checks or unchecks whether the rigidbody is affected by the gravity system.
Is Kinematic is used to allow Objects that have it checked to act on other objects using the Physics system but not themselves be affected by the physics system. This also affects the animation system in which the kinematic object is controlled by the animation system but not the physics system.
Interpolate controls how collision detection is handled, either by calculating the previous frames’ movements in the animation timeline (Interpolate) or by guessing the next frame (Extrapolate) using some mathematical calculations and assumptions.
Collision Detection sets the rate at which collisions are detected as discrete, continuous, and continuous dynamic, and continuous speculative.
Discrete is the default mode and should be your choice unless you have a reason to change it. Continuous covers interaction of dynamic objects with static objects, continuous dynamic covers dynamic objects with dynamic objects, and continuous speculative covers predictive collision detection.
Last are constraints, these are useful for controlling the position or rotation of an object. For example you may want to rotate a camera on a certain axis but not moving the position.
Frictionless: Physics Materials
These are custom material types that allow for physical properties for collider objects such a friction, and bounciness and are applied to the Object’s material property in the inspector. You can explore more about physics materials (here) in the manual but there are five values that govern it.
Dynamic Friction: Friction used when moving.
Static Friction: Friction used when at rest.
Bounciness: How bouncy the surface is.
Friction Combine: How two friction objects are combined.
Bounce Combine: How two bouncy objects are combined.
Double Jointed: Physics Joints
Joints allow two rigidbodies or a rigidbody and a fixed point in space to connect to one another. This can allow movement and restrict movement of the joint depending on the parameters chosen as well as apply force on rigidbodies. There are several kinds of joints such as Character, Configurable, Spring, Hinge and Fixed.
Configurable Joint: Mimics a skeletal joint most often used in ragdolls. Can restrict and force rigidbody movement in any fashion.
Spring Joint: Acts as an elastic between two rigidbodies pulling toward the same point but also provides tension to keep them apart.
Character Joint: Emulates a joint such as an elbow or knee. Only allows linear movement of the joint as well as total angular freedom and rigidbodies are connected around each axis and share an origin.
Fixed Joint: Good for connecting two rigidbodies without parenting to a shared transform also useful for objects designed to break apart. Follows the movement of the rigidbody it is affixed to.
Hinge Joint: Attaches to another rigidbody or a point in space. Useful for emulating a door or an elbow as it rotates around a specific axis from the origin point.
Layered Up: Layers & Collision Matrix
By default when creating physics everything is automatically assigned to the Default layer in the Layer and Collision Matrix, you can better fine tune physics reactions by creating and adjusting different layers for your collisions.
You can reach this Layer/Collision Matrix by going to Edit > Project Settings > Physics (or Physics 2D) at the bottom of Physics Settings
You can also learn more about Layer Based Collision (here) in the Unity Manual.
Keep It Steady: Fixed Update
Another important feature to be aware of is Fixed Update. Generally speaking Fixed Update (not Update or Late Update) is where you want to do your physics calculations.
You can learn more (here) in the Unity Scripting API but generally unlike Update which happens every frame (which can vary greatly computer to computer) and Late Update which happens absolutely last.
Fixed Update happens at a fixed time interval (like the name implies) that you can change at Edit > Settings > Time > Fixed Timestep but defaults at 0.02 seconds or 50 times a second. This creates predictable, consistent, and reliable physics calculations.
Laser Focus: Raycasting
Raycasting creates an invisible ray (or line) from one Physics object to another. This is useful for getting distance, firing a projectile, getting an collision, determining distance of a fall, or any number of useful details. Refer to the Unity Scripting API (here) for implementation details. It is best to use raycasts outside of Fixed Update, sparingly in a scene, and only with primitive colliders due to their computational expense.
This article covered but the most basic features of the Unity Physics system, however using these tools you can create dynamic objects that simulate approximately forces of gravity, velocity, friction, and force to make your games immersive and emergent. Until next time, happy coding.