TExcelTAD
Home › Unity with C# › Rigidbody & Forces

Rigidbody & Forces

A Rigidbody puts a GameObject under the control of the physics engine (PhysX for 3D in Unity). It gets mass, velocity, gravity and collision response. From then on, the physics engine owns its position: you influence it with forces and velocities, and you do it in FixedUpdate.

Migrating: Unity 6 renamed Rigidbody.velocity to linearVelocity, drag to linearDamping and angularDrag to angularDamping. The old names are obsolete. 2D physics (Rigidbody2D) made the same linearVelocity rename.

Key settings

Setting Effect
Mass in kilograms by convention; affects forces and collisions, not falling speed
Linear / Angular Damping slows movement and spin every step (air resistance, friction-like feel)
Use Gravity applies Physics.gravity (default (0, -9.81, 0))
Is Kinematic moved only by your code (MovePosition), pushes others but isn't pushed
Interpolate smooths rendering between physics steps (use it on the player and the camera's target)
Collision Detection Discrete (default), Continuous, Continuous Dynamic, Continuous Speculative: prevents fast objects tunneling through thin colliders
Constraints freeze position or rotation axes, such as rotation X and Z for an upright character

Moving a Rigidbody correctly

csharp
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody))]
public class HoverShip : MonoBehaviour
{
    [SerializeField] float thrust = 20f, maxSpeed = 12f, boostImpulse = 8f;
    Rigidbody body;
    InputAction move, jump;
    Vector2 input;
    bool boostQueued;

    void Awake()
    {
        body = GetComponent<Rigidbody>();
        body.interpolation = RigidbodyInterpolation.Interpolate;
        move = InputSystem.actions.FindAction("Move");
        jump = InputSystem.actions.FindAction("Jump");
    }

    void Update()
    {
        input = move.ReadValue<Vector2>();
        if (jump.WasPressedThisFrame()) boostQueued = true;
    }

    void FixedUpdate()
    {
        body.AddForce(new Vector3(input.x, 0f, input.y) * thrust, ForceMode.Acceleration);

        if (boostQueued)
        {
            body.AddForce(transform.forward * boostImpulse, ForceMode.VelocityChange);
            boostQueued = false;
        }

        // Clamp horizontal speed without touching vertical (gravity) speed.
        Vector3 v = body.linearVelocity;
        Vector3 flat = Vector3.ClampMagnitude(new Vector3(v.x, 0f, v.z), maxSpeed);
        body.linearVelocity = new Vector3(flat.x, v.y, flat.z);
    }
}

ForceMode decides how the vector is applied:

Mode Change in velocity this step Mass matters? Typical use
Force (default) F · dt / m yes engines, wind, continuous pushing
Acceleration F · dt no continuous movement that feels the same for any mass
Impulse F / m yes explosions, hits, jumps of objects with real mass
VelocityChange F no instant dashes, jumps tuned in m/s

The model applies each mode once to a 2 kg body for one 0.02 s step, then simulates a fall with Unity-like semi-implicit Euler integration:

csharp · try it yourself
using System;

const double Mass = 2.0, Dt = 0.02;
double F = 10;
Console.WriteLine(
quot;Force: dv = {F * Dt / Mass:F3} m/s"); Console.WriteLine(
quot;Acceleration: dv = {F * Dt:F3} m/s"); Console.WriteLine(
quot;Impulse: dv = {F / Mass:F3} m/s"); Console.WriteLine(
quot;VelocityChange: dv = {F:F3} m/s"); // Jump with VelocityChange 5 m/s up, gravity -9.81, 50 Hz. double y = 0, vy = 5, peak = 0; int steps = 0; do { vy += -9.81 * Dt; // velocity first... y += vy * Dt; // ...then position (semi-implicit Euler) peak = Math.Max(peak, y); steps++; } while (y > 0); Console.WriteLine(
quot;jump: peak {peak:F3} m, lands after {steps} steps ({steps * Dt:F2} s)"); Console.WriteLine(
quot;analytic peak {5 * 5 / (2 * 9.81):F3} m, airtime {2 * 5 / 9.81:F2} s");
Force:          dv = 0.100 m/s
Acceleration:   dv = 0.200 m/s
Impulse:        dv = 5.000 m/s
VelocityChange: dv = 10.000 m/s
jump: peak 1.225 m, lands after 50 steps (1.00 s)
analytic peak 1.274 m, airtime 1.02 s

The simulated peak is a little lower than the formula, which is normal for a fixed-step integrator. Tune jumps in the engine, by feel, not on paper.

Why not move it with transform?

Setting transform.position on a non-kinematic Rigidbody teleports it. The physics engine sees no movement in between, so it can end up inside or past a wall, and it resets contacts and wakes everything up. The model moves a fast object (1.5 m per step) toward a 0.5 m thick wall at x = 10:

csharp · try it yourself
using System;

const double WallMin = 10.0, WallMax = 10.5, Step = 1.5;

double x = 6;
Console.Write("transform teleport: ");
for (int i = 0; i < 5; i++)
{
    x += Step;
    bool inside = x >= WallMin && x <= WallMax;
    Console.Write(
quot;{x:F1}{(inside ? "(hit)" : "")} "); } Console.WriteLine("-> passed through the wall"); x = 6; Console.Write("swept (continuous): "); for (int i = 0; i < 5; i++) { double next = x + Step; if (x < WallMin && next >= WallMin) { x = WallMin; Console.Write(
quot;{x:F1}(hit) "); break; } // sweep the path x = next; Console.Write(
quot;{x:F1} "); } Console.WriteLine("-> stopped at the wall");
transform teleport: 7.5 9.0 10.5(hit) 12.0 13.5 -> passed through the wall
swept (continuous): 7.5 9.0 10.0(hit) -> stopped at the wall

Even the teleport "hit" at 10.5 is just luck: with a 1.6 m step it would never touch the wall. That is tunneling, and Continuous collision detection fixes it by sweeping, as in the second loop.

Rules:

  1. Dynamic bodies: change linearVelocity or call AddForce in FixedUpdate.
  2. Kinematic bodies (moving platforms, doors): call body.MovePosition(p) / MoveRotation(r) in FixedUpdate, so physics computes their velocity and pushes riders correctly.
  3. Use body.position (not transform.position) when you must teleport, for respawns.

Test yourself

Which Unity 6 property replaces Rigidbody.velocity?

  1. speed
  2. linearVelocity
  3. moveVelocity
  4. velocity3D

You want a dash that adds exactly 8 m/s regardless of mass. Which ForceMode?

  1. Force
  2. Impulse
  3. VelocityChange
  4. Acceleration

How should a kinematic moving platform be moved?

  1. transform.position in Update
  2. Rigidbody.MovePosition in FixedUpdate
  3. AddForce in LateUpdate
  4. By animating its collider only