Rigidbody
TypeClass (Component)
NamespaceUnityEngine
InheritsComponent > Object
2D versionRigidbody2D

Rigidbody

Adding a Rigidbody hands an object over to the physics engine. It now has mass, falls with gravity, gets pushed by other objects and can be moved with forces. It needs a Collider to actually bump into things.

Without a Rigidbody, your script is a puppeteer holding the object on strings: it goes exactly where you put it. With a Rigidbody, you cut the strings and hand it to the laws of physics. You can still influence it, like kicking a ball (AddForce) or setting how fast it's rolling (linearVelocity), but if you grab it and teleport it (transform.position), physics gets confused.

Properties

NameTypeDescription
linearVelocityVector3Current speed and direction in units per second. (Called velocity before Unity 6.)
angularVelocityVector3Spin speed, in radians per second.
massfloatWeight in kilograms (roughly). Heavier objects need more force to move. Mass doesn't change how fast things fall.
linearDampingfloatAir resistance. Higher = slows down faster. (Called drag before Unity 6.)
angularDampingfloatResistance to spinning. (Called angularDrag before Unity 6.)
useGravityboolWhether gravity affects it.
isKinematicboolIf true, physics won't move it; you do, using MovePosition. It still pushes other objects.
constraintsRigidbodyConstraintsLock movement or rotation on certain axes, e.g. RigidbodyConstraints.FreezeRotation so a character doesn't tip over.
interpolationRigidbodyInterpolationSet to Interpolate on the player or anything the camera follows, to remove jitter.
collisionDetectionModeCollisionDetectionModeContinuous stops fast objects from passing through walls. Costs more.
position, rotationVector3, QuaternionThe physics engine's own position/rotation for the body.

Methods

MethodReturnsDescription
AddForce(Vector3 force, ForceMode mode = ForceMode.Force)voidPush the object. See ForceMode below.
AddTorque(Vector3 torque, ForceMode mode)voidSpin the object.
AddForceAtPosition(force, position)voidPush at a specific point, which also causes spin (hitting the edge of a crate).
AddExplosionForce(force, center, radius)voidPush away from a point, weaker further away.
MovePosition(Vector3 position)voidMove to a position during the next physics step. The right way to move kinematic bodies.
MoveRotation(Quaternion rotation)voidRotate during the next physics step.
Sleep() / WakeUp()voidResting bodies "sleep" to save CPU. They wake up automatically when hit.

ForceMode: which one do I use?

ModeUses mass?TimingGood for
Force (default)YesContinuous: call every FixedUpdateEngines, wind, steady pushing
AccelerationNoContinuousCustom gravity, pushing everything equally
ImpulseYesOnce, instantlyJumps, explosions, getting hit
VelocityChangeNoOnce, instantlyDashes that feel the same for any mass

Code Samples

Physics-based player with a jump

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PhysicsPlayer : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 6f;
    [SerializeField] private float jumpForce = 5f;

    private Rigidbody rb;
    private Vector2 moveInput;
    private bool jumpRequested;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        rb.constraints = RigidbodyConstraints.FreezeRotation; // don't fall over
    }

    void Update()
    {
        // 1. Read input in Update (so no key press is missed).
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        if (Input.GetButtonDown("Jump")) jumpRequested = true;
    }

    void FixedUpdate()
    {
        // 2. Apply physics in FixedUpdate.
        Vector3 move = new Vector3(moveInput.x, 0f, moveInput.y).normalized * moveSpeed;
        // Keep current vertical speed so gravity and jumping still work.
        rb.linearVelocity = new Vector3(move.x, rb.linearVelocity.y, move.z);

        if (jumpRequested)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            jumpRequested = false;
        }
    }
}

[RequireComponent(typeof(Rigidbody))] makes Unity add a Rigidbody automatically when you add this script, and prevents it from being removed.

A moving platform (kinematic)

using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
    [SerializeField] private Vector3 offset = new Vector3(0f, 0f, 5f);
    [SerializeField] private float speed = 1f;
    private Rigidbody rb;
    private Vector3 start;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true;
        start = rb.position;
    }

    void FixedUpdate()
    {
        float t = Mathf.PingPong(Time.time * speed, 1f);
        rb.MovePosition(start + offset * t);
    }
}

What happens at runtime

  1. AddForce doesn't move anything right away. It adds to a total that's applied during the next physics step.
  2. Each physics step (every Time.fixedDeltaTime seconds, 0.02 by default): gravity is added to velocity, forces are converted to velocity changes (dividing by mass where relevant), then position is moved by velocity × time.
  3. The engine checks for overlaps and pushes objects apart, then calls collision messages.
  4. Unity writes the new position into the Transform. With Interpolate on, the Transform is smoothed between physics steps so it looks fluid at high frame rates.

Common Mistakes

See also