0
0
Unityframework~5 mins

Rigidbody forces and velocity in Unity

Choose your learning style9 modes available
Introduction

Rigidbody forces and velocity let you move objects in a game realistically. They help objects react to pushes, pulls, and speed changes like in real life.

When you want a ball to roll after being hit.
When making a character jump or fall naturally.
When simulating wind pushing leaves or objects.
When you want objects to bounce or slide on surfaces.
When controlling a car or vehicle movement with physics.
Syntax
Unity
Rigidbody rb;

// Add a force to the Rigidbody
rb.AddForce(Vector3 force, ForceMode mode = ForceMode.Force);

// Set the velocity directly
rb.velocity = new Vector3(x, y, z);

AddForce applies a push or pull over time, affected by mass.

Velocity sets the speed and direction instantly, overriding current movement.

Examples
This pushes the object upward with a default force mode.
Unity
rb.AddForce(new Vector3(0, 10, 0));
This applies an instant push to the right like a quick hit.
Unity
rb.AddForce(new Vector3(5, 0, 0), ForceMode.Impulse);
This sets the object's speed upward directly, ignoring forces.
Unity
rb.velocity = new Vector3(0, 5, 0);
Sample Program

This script moves an object with a Rigidbody. Press Space to jump up with a quick force. Press V to move right by setting velocity directly.

Unity
using UnityEngine;

public class MoveObject : MonoBehaviour
{
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Apply an instant upward force
            rb.AddForce(new Vector3(0, 300, 0), ForceMode.Impulse);
        }

        if (Input.GetKeyDown(KeyCode.V))
        {
            // Set velocity directly to move right
            rb.velocity = new Vector3(5, rb.velocity.y, 0);
        }
    }
}
OutputSuccess
Important Notes

Use AddForce for natural pushes that respect mass and drag.

Use velocity to instantly change speed, but it can ignore physics effects.

Always get the Rigidbody component before applying forces or velocity.

Summary

Rigidbody forces push or pull objects over time.

Velocity sets object speed instantly.

Use forces for realistic physics and velocity for direct control.