Rigidbody Mass in Unity
Introduction
Understanding the concept of mass in Unity’s Rigidbody component is essential for creating realistic physics simulation in a Unity game. In this article, we will discuss the role of mass in Rigidbody physics, specifically its impact on collisions and gravity, and how to manipulate it in the Unity Inspector and through scripting.
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
What is Mass?
In Unity, mass is a property of the Rigidbody component, representing how heavy an object is. It influences how the object interacts with forces and other physics objects in the game world. The higher the mass, the more force it takes to move the object.
Mass of a Rigidbody is a floating point and can be set to any value either using the Unity’s inspector or using script. Just keep in mind that large variations in Rigidbody mass between multiple objects in a scene can make the physics simulation unstable.
Mass and Rigidbody Behavior in Collisions
In a collision, the mass of an object often gives the same outcome as in the real world. An object with a higher mass will exert a greater force upon collision, making it less prone to changes in its motion. This is important in games where realistic physics interactions are required. For instance, a bowling ball (mass value of 20) will push a tennis ball (mass value of 5) far away upon collision due to its greater mass.
Mass and Rigidbody Behavior with Gravity
In contrast to common assumptions, mass does not affect how gravity influences a Rigidbody in Unity. All gameobjects, regardless of their mass, fall at the same rate if only gravity is acting on it.The acceleration due to gravity remains constant and is not affected by the mass of the object.
This means a feather and a hammer will fall at the same rate in a vacuum. However, the motion is affected by other Rigidbody parameters like drag.
Changing a Rigidbody’s Mass in the Inspector
To adjust an object’s mass in Unity, select the object in the Hierarchy window, go to the Inspector window, and find the Rigidbody component. Here, you’ll find the ‘Mass’ property, which can be modified to your desired value. This is a straightforward method for changing the value of mass during the game development process.
Changing a Rigidbody’s Mass Using Scripting
If mass needs to be updated during runtime, then it can be altered via a simple script. This allows for real-time changes in mass based on game mechanics or player actions.
As in case of other components in Unity, the Rigidbody component needs to be accessed using GetComponent<> before assigning the mass.
Here’s a basic C# script to change a Rigidbody’s mass
using UnityEngine;
public class RigidbodyMassController : MonoBehaviour
{
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
UpdateMass(5.0f);
}
public void UpdateMass(float newMass)
{
rb.mass = newMass;
}
}
Conclusion
Mastering the concept of mass in Rigidbody components is critical for creating realistic physics in Unity games. Without the right understanding, a Rigidbody might end up not moving just because the mass was too high for the force applied.
Whether through the Inspector or scripting, understanding and manipulating mass can lead to more dynamic physics game environments.