Unity's Physics System: Rigidbodies and Colliders
Tired of your meticulously crafted explosions clipping through walls? This guide will help you master Unity’s Rigidbodies and Colliders.
Introduction to Unity’s Physics Engine
Unity uses the PhysX engine for physics simulations. This engine handles movement, collisions, and physical interactions. The physics engine simulates object behavior, whether realistic or stylized.
Unity separates 2D and 3D physics, each with distinct components and behaviors. Effective physics are key to engaging gameplay. Perhaps you’re looking for assets to help get you started. If so, you can find unlimited game assets to use in your projects at Strafekit. You can find all kinds of assets, including something to house your game, such as a Modern Urban House.
Understanding Rigidbodies
A Rigidbody makes a GameObject subject to the physics engine. Adding one turns a static object into a dynamic one, affected by forces like gravity.
To add a Rigidbody, select a GameObject, click “Add Component,” and search for “Rigidbody” (or “Rigidbody 2D” for 2D projects).
Rigidbody properties define how an object interacts with the physics world.
Mass dictates an object’s resistance to movement. Drag slows an object’s movement through the air.
Angular Drag slows its rotation. “Use Gravity” toggles gravity’s effect.
“Is Kinematic” makes the Rigidbody immune to physics forces. Move it directly via its Transform
, useful for character controllers.
Collision Detection dictates how the Rigidbody detects collisions (Discrete, Continuous). Constraints freeze movement or rotation along specific axes.
Rigidbody Interpolation smooths movement between physics updates. Collision Detection modes balance accuracy and performance.
Exploring Colliders
A Collider defines a GameObject’s shape for physical collisions. Without one, a Rigidbody passes through other objects.
Unity offers several Collider types:
- Box Collider: A rectangular prism.
- Sphere Collider: A sphere.
- Capsule Collider: A capsule, ideal for characters.
- Mesh Collider: Uses the object’s mesh (performance-intensive).
- Terrain Collider: For Unity Terrain objects.
- Wheel Collider: For vehicle prefabs.
Collider properties define how the object interacts with other colliders. “Is Trigger” disables physical collisions, triggering events instead. Material assigns a Physics Material for friction and bounciness.
Bounding volumes are used for broad-phase collision detection. This quickly narrows down potential collisions before more precise checks are performed.
Compound Colliders combine primitive Colliders (Box, Sphere, Capsule) for complex shapes. This beats a Mesh Collider for performance.
Rigidbody and Collider Interactions
Rigidbodies and Colliders: they’re a team! A Rigidbody controls the what (movement), while a Collider defines the where (collision shape).
Collision events trigger when Colliders (one with a Rigidbody) make contact:
- OnCollisionEnter: Collision begins.
- OnCollisionStay: During collision.
- OnCollisionExit: Collision ends.
Trigger events occur when a Collider marked “Is Trigger” overlaps another:
- OnTriggerEnter: Trigger entered.
- OnTriggerStay: Trigger active.
- OnTriggerExit: Trigger exited.
Use these events for gameplay logic: damage, item collection, and level transitions.
Kinematic Rigidbodies vs. Dynamic Rigidbodies
Kinematic Rigidbodies ignore physics forces. Move them by setting their Transform
.
Dynamic Rigidbodies are fully physics-controlled, reacting to forces and collisions.
Use Kinematic Rigidbodies for:
- Character controllers (for precise movement).
- Moving platforms (for consistent movement).
Use Dynamic Rigidbodies for:
- Objects needing realistic physics (falling objects, projectiles).
Control Kinematic Rigidbodies with Rigidbody.MovePosition
and Rigidbody.MoveRotation
for smooth movement.
Physics Materials
Physics Materials define friction and bounciness. They control how objects interact on contact.
Properties include:
Static Friction: Friction when an object is still. Dynamic Friction: Friction when moving.
Bounciness: Energy conserved in a collision. High bounciness creates highly elastic collisions.
Friction Combine: Determines how the friction of two colliding objects is combined.
- Average: Averages the friction.
- Min: Uses the lower value.
- Max: Uses the higher value.
- Multiply: Multiplies them.
Bounce Combine: Determines how the bounciness of two colliding objects is combined. It uses the same methods as Friction Combine: Average, Min, Max, and Multiply.
To create one, right-click in the Project window, select “Create,” then “Physics Material” (or “Physics Material 2D”). Adjust properties and assign it to a Collider’s material
.
High friction creates surfaces that resist sliding.
Best Practices and Optimization
Optimize physics, especially in complex scenes.
Avoid excessive use of Mesh Colliders, which can be performance-intensive; favor primitive Colliders or Compound Colliders.
Use Collision Layers and the Collision Matrix (Edit -> Project Settings -> Physics) to make sure only relevant objects collide. This reduces the number of collision checks the physics engine must perform each frame.
The Fixed Timestep (Edit -> Project Settings -> Time) dictates physics update frequency. Balance accuracy and performance. Use the Unity Profiler (Window -> Analysis -> Profiler) to find bottlenecks, looking for spikes in “Physics” or “Physics 2D.”
Advanced Physics Techniques
Applying forces and torques allows controlled movement.
Rigidbody.AddForce
applies continuous force. Rigidbody.AddTorque
applies rotational force.
Rigidbody.AddForce(force, ForceMode.Impulse)
applies an instant force. Use impulse forces to create a “cannon” effect, instantly propelling an object.
Implement custom physics by scripting modifications to a Rigidbody’s velocity or applying forces based on conditions. To further help with game development, you can check out Wayline. While working on your physics, you may need different skyboxes. Consider checking out the 55 HDRIs Anime Pure Sky Skybox asset! If you are an indie developer you should know that indie game development means balancing creativity with technical skills. Understanding and managing these limitations is key to successful projects, and further reading on how do indie game designers balance creativity and technical limitations might be helpful.
Raycasting detects collisions along a line. Use Physics.Raycast
(or Physics2D.Raycast
) to check for objects.
For example, use raycasting to implement a simple targeting system. It’s also useful for ground detection and AI.