Unity6 Game Development · Lesson 6

Rigidbody forces belong to the physics step

Use AddForce with an explicit ForceMode instead of moving a dynamic body through Transform.

Unity 6 Editor walkthrough

The Unity script needs the Editor and the setup listed below. The separate C# companion practice runs in SharpRune without UnityEngine.

Let physics own a dynamic Rigidbody’s motion

A dynamic Rigidbody participates in Unity’s physics simulation, so its movement policy should use the physics API rather than repeatedly competing with it through Transform changes. ForceMover caches its body in Awake and applies an acceleration from FixedUpdate. The setup disables gravity and leaves the body nonkinematic to isolate that effect. This is an intentionally narrow experiment: it does not claim every controller must use continuous forces, but it requires choosing one supported movement model and keeping its ownership of the pose understandable.

Interpret ForceMode before adding a time factor

The real AddForce call uses Vector3.right multiplied by the configured acceleration and ForceMode.Acceleration. In that mode, the argument represents acceleration independent of the body’s mass, and Unity applies the relevant simulation-step effect. Multiplying this argument by deltaTime again is not a universal rule and can unintentionally weaken the intended continuous effect. AddForce accumulates its requested velocity change for simulation; immediately reading a value beside the call is not the same as observing the body after the physics step has been processed.

Compare two elementary rate calculations honestly

The portable Integrate function begins at speed zero and applies two half-second changes. Acceleration four for one total second produces speed four, printed as 4.00. Dividing force four by mass two first produces acceleration two, which integrates to speed two, printed as 2.00. These are controlled decimal arithmetic results. They do not reproduce collision response, damping, solver behavior, gravity or Unity’s floating-point simulation. Their purpose is to distinguish a rate quantity from a force-to-acceleration conversion before choosing the matching engine ForceMode.

The four force modes do not share one unit contract

Force includes mass and a time-step effect, Acceleration omits mass dependence while still being continuous, and the impulse-oriented modes use different time treatment. A numeric constant copied between modes therefore need not produce the same motion. Also remember that a kinematic or inactive body is not the nonkinematic active setup described here. A missing effect should be diagnosed through body state, mode, simulation configuration and competing movement scripts, rather than fixed by multiplying by arbitrary time factors until one scene appears to move.

Practice: hold the mode fixed while comparing mass

Run the isolated sphere with Acceleration and compare mass one with mass two in separate runs. Keep gravity, damping and other settings unchanged. Then use a copy with Force mode and compare the mass-dependent response. Do not change mass, mode and time factor simultaneously, because that prevents attributing the observation to one contract change. Inspect movement over actual simulated time and record settings; no exact position at an arbitrary wall-clock observation is promised by the supplied snippet.

  • Keep direct Transform movement off the same dynamic body during this experiment.
  • Record fixed timestep, body state and mode beside any velocity or position observation.

Practice: change force and duration independently

For an independent C# exercise, double the number of half-second steps and predict speeds eight and four for the two supplied acceleration values. Then keep total elapsed time one second while using smaller step partitions, explaining why the simple constant-acceleration arithmetic agrees. Add a positive-mass validation guard to a separate force-to-acceleration helper. This isolates basic units and domain checks without pretending the companion contains a physics engine or that exact decimal integration proves every discrete game simulation has identical numerical behavior.

  • Reject a zero or negative mass under the chosen arithmetic contract instead of dividing blindly.
  • Keep the comparison about speed change, not an unstated position integration rule.

Match callback, units and body configuration

A defensible force-based controller states when input is sampled, when physics work is applied, what units the chosen mode consumes, and which body state is supported. This lesson supplies the real script and scene setup, while the portable lab only checks the related arithmetic. Editor and Player integration must still be tested with the configured Rigidbody and collisions. Treat timestep changes, damping and competing movement as explicit variables rather than assuming the presence of FixedUpdate alone guarantees correct or frame-rate-independent motion.

  • Portable baseline outputs are 4.00 and 2.00.
  • The Unity script applies continuous mass-independent acceleration through AddForce.
  • ForceMode determines whether mass and time scaling belong in the engine operation.
Runs in Unity 6 Editor

Unity Editor example

Set up in Unity 6

  1. Create a Sphere, add ForceMover.cs and inspect the required Rigidbody.
  2. Disable Use Gravity and keep Is Kinematic off; place the sphere away from other colliders.
  3. Enter Play mode and inspect its rightward motion and Rigidbody linear velocity.
  4. Compare mass 1 and mass 2 using the same Acceleration mode in separate runs.
  5. In a copy, change only the mode to Force and compare the mass-dependent response; do not also move Transform.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public sealed class ForceMover : MonoBehaviour
{
    [SerializeField] private float acceleration = 4f;
    private Rigidbody body;
    private void Awake() { body = GetComponent<Rigidbody>(); }
    private void FixedUpdate()
    {
        body.AddForce(Vector3.right * acceleration, ForceMode.Acceleration);
    }
}

What to observe in Unity

The dynamic body accelerates rightward. Acceleration mode is mass-independent, while Force mode uses mass. The script does not multiply its acceleration by deltaTime.

The console practice below isolates the C# logic. It does not run a Unity scene or emulate Unity engine callbacks.

C# companion practice

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

decimal Integrate(decimal acceleration) { decimal speed = 0; for (int i = 0; i < 2; i++) speed += acceleration * 0.5m; return speed; }
Console.WriteLine(Integrate(4m).ToString("0.00", CultureInfo.InvariantCulture));
Console.WriteLine(Integrate(4m / 2m).ToString("0.00", CultureInfo.InvariantCulture));
Open C# practice

C# practice output

4.00
2.00
Try this
ForceMode determines whether mass and the simulation step are applied by Unity.

Further reading