Overview of Physics in Unity
Without physics, there is no game development. Physics in Unity is the building block for game development in Unity. It defines the interaction between game objects. Physics in Unity is provided by the Unity Physics Engine.
This is a series on Unity Physics. We recommend reading the series in order.
- Unity Physics
- Unity Collision Detection
- Character Controller and Input Systems
- Rigidbody Mass in Unity
- How to add Friction to a Rigidbody
- Cylinder Collider in Unity
- Box Collider in Unity
- Box Cast in Unity
- Sorting Layers
- Get the distance between two objects
- How to normalize a vector
Key Concepts in Unity Physics Engine
In this article, you will learn about key concepts in the Unity Physics Engine.
Colliders
This indicates when characters collide with each other. For collisions to occur, each character must have a RigidBody component. There are several types of colliders. They include:
- Box collider
- Sphere collider
- Wheel collider
- Cylinder collider
- Capsule collider
- Mesh collider
To add a collider to a Unity game object, click on Add Component from the inspector window, select Physics, and choose the collider you want to use from the drop-down.
π Resources:
- Collider Interactions in Unity
- Using BoxCast in Unity
- Rigidbody Mass in Unity
- Get the Distance between Two Objects in Unity
Triggers
Triggers allow characters to pass through it by disabling physics on the component. That is, characters can enter or exit the trigger. Just like colliders, the characters must have a RigidBody component to move within the trigger. To activate this component, you have to check the Is Trigger checkbox on the collider.
Rigidbody
Now, we have about this particular physics as an essentiality for the above two physics. What is RigidBody itself?The RigidBody component is the component that gives the effect of physics properties to game objects. Physical properties like mass, velocity, drag, and gravity. It has an Is Kinematic checkbox which allows the physics to affect other objects but not itself. There is also an Interpolate select box that checks collisions. You can either choose Interpolate or Extrapolate. Another select box is Collision Detection which checks the rate of collisions. You can choose Discrete, Continuous, Continuous Dynamic, or Continuous Speculative. There is also a Constraints drop-down.
To access this component, click on Add Component from the inspector window, select Physics, and choose RigidBody from the drop-down.
Physics Materials
These are components that control the surface friction and bounciness. They include:
- Bouncy
- Ice
- Max Friction
- Metal
- Rubber
- Wood
- Zero Friction
Joints
There are different types of joint components in the Unity Physics Engine. They include:
- Fixed Joint component β as the name implies, this component creates a permanent joint between two game objects.
- Spring Joint component β this component moves the game objects in a spring-like manner.
- Hinge Joint component β this component is similar to the previous one but moves the game objects around a pivot.
Scripting
These are functions for interacting with game objects. They include:
- OnCollisionEnter β to register a collision.
- OnCollisionStay β during a collision.
- OnCollisionExit β to exit a collision.
- useGravity β to enable or disable gravity.
- AddForce β to add a force in a particular direction.
- AddTorque β to add a rotational force.
π Resources:
Layers and Collision Matrices
The collision matrix helps determine the relationship between collision layers. It can be found in the Physics Engine settings. To open this, go to Edit in the menu drop-down, select Project Settings, then Physics. The collision matrix will be seen at the bottom of that window.
Conclusion
Apart from the physics components in the Unity Physics Engine listed above, there are others. However, the ones listed above are the basic physics components you need to know to start your game development with Unity.To learn more about the Unity Physics Engine, visit Unityβs official docs here.