Overview
One of Unity's powerful features is its built-in physics engine (NVIDIA PhysX) for easy realistic physics simulation. Objects falling due to gravity, accelerating when force is applied, bouncing off each other on collision—all these real-world behaviors are calculated by this physics engine.
Rigidbody is the component that places GameObjects under physics engine control, making them behave as "physics-enabled objects." Unlike directly manipulating the Transform component, using Rigidbody enables more realistic and dynamic movement.
This article covers the basic role of the Rigidbody component and proper methods for moving objects using Rigidbody.
The Role of Rigidbody
Adding a Rigidbody component to a GameObject makes it a "mass-bearing object" subject to physical effects:
- Gravity: With
Use Gravityenabled, objects automatically fall toward gravity (default: negative Y-axis direction). - Forces and Torque: Apply
AddForcefrom scripts to accelerate,AddTorqueto rotate. - Collision: Collides with other
RigidbodyorColliderobjects, producing realistic rebounds and pushes based on mass and velocity. - Drag: Set
Drag(air resistance) andAngular Drag(rotational resistance) to express movement damping.
transform vs Rigidbody: Moving Objects
The most confusing point for beginners is the difference between moving with transform.Translate() versus Rigidbody.
-
transform.Translate(): Essentially "teleports" the object by the specified amount. Completely ignores physics laws, potentially causing objects to clip through walls or pass through other objects. Suitable for simple movement without physics interactions, or objects outside physics engine control (e.g., camera movement). -
Rigidbodyoperations: Moves objects by applying force or setting velocity directly. All movement is calculated and managed by the physics engine, producing realistic collisions and interactions with other objects. Objects requiring physics behavior should always be moved throughRigidbody.
Warning: Directly manipulating the transform of objects with Rigidbody causes conflicts with physics engine calculations, resulting in teleportation and unnatural collisions. Avoid mixing both approaches.
Moving Objects with Rigidbody
To operate a Rigidbody, first get its reference with GetComponent<Rigidbody>() and process in FixedUpdate().
1. Applying Force with AddForce()
AddForce() is the most common method for continuously applying force to accelerate objects. It's analogous to "pushing" in the real world.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public float moveForce = 10f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// Apply force to Rigidbody
rb.AddForce(movement * moveForce);
}
}
AddForce has an optional ForceMode argument specifying how force is applied:
ForceMode.Force: Continuous force considering mass (default).ForceMode.Acceleration: Continuous force ignoring mass.ForceMode.Impulse: Instantaneous impulse considering mass (for explosions, etc.).ForceMode.VelocityChange: Instantaneous velocity change ignoring mass.
2. Setting Velocity Directly
Directly modifying rb.velocity sets the object's velocity immediately. Useful for snappier character control.
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// Preserve Y velocity while setting X, Z velocity
rb.velocity = new Vector3(movement.x * speed, rb.velocity.y, movement.z * speed);
}
Key Properties
- Mass: Object mass. Heavier objects are harder to move and have greater impact on other objects.
- Drag: Air resistance during movement. Higher values cause faster velocity decay.
- Angular Drag: Rotational resistance. Higher values stop rotation faster.
- Use Gravity: Whether to be affected by gravity.
- Is Kinematic: When enabled, the object ignores physics engine forces (gravity, collisions). However, moving via
transformcan still physically affect otherRigidbodyobjects (non-kinematic). Used for animation-driven characters or script-controlled platforms. - Constraints: Freeze movement or rotation on specific axes. Useful for preventing 2D characters from moving on Z-axis, or stopping player characters from toppling by freezing X, Z rotation.
Summary
Rigidbody is the protagonist of Unity's physics world.
- Adding
Rigidbodymakes objects obey physics laws. - Move physics-enabled objects through
Rigidbody, nottransform. - Use
AddForce()for continuous force,velocityfor direct speed control. - Write processing in
FixedUpdate()—this is essential. Is Kinematicis a special mode for affecting others without receiving physics.
Mastering Rigidbody is the key to creating realistic, compelling interactions.