Course contentsShow
← Unity Game Development
Lesson 155 of 8283. Physics and InteractionFree lesson

Applying Physical Forces and Torques via Script

You will learn to manipulate dynamic bodies programmatically by applying forces and rotational torque to create responsive gameplay mechanics.

In this lesson, you will learn how to move and rotate objects dynamically by injecting physical energy directly into their Rigidbody components.

Understanding Forces and Torques

Up until now, you have configured physics settings and handled collision triggers. Now, we are moving into the "active" phase of physics: applying external input to move objects realistically.

Think of a Force as a push or a pull applied to a specific point on an object. If you push a ball at its center, it moves in a straight line; if you push it off-center, you naturally create a rotation. Torque, on the other hand, is rotational force. Applying torque is the equivalent of "spinning" an object around its axis, such as a wheel or a spinning top.

Applying Physics in Script

Because forces interact with the physics engine, it is critical to apply them during the FixedUpdate cycle—the same heartbeat that governs the physics simulation pipeline you learned earlier. Using Update for physics can lead to jittery or inconsistent behavior because it runs at variable frame rates.

Here is a basic example of how to apply both force and torque to a Rigidbody:

public class PhysicsController : MonoBehaviour
{
    private Rigidbody rb;
    public float forceMagnitude = 10f;
    public float torqueMagnitude = 5f;

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

    void FixedUpdate()
    {
        // Apply a forward force to the object
        rb.AddForce(transform.forward * forceMagnitude);

        // Apply a rotational torque around the y-axis
        rb.AddTorque(Vector3.up * torqueMagnitude);
    }
}

Choosing the Right Force Mode

When calling AddForce, you can specify a ForceMode. The default is ForceMode.Force, which treats the input as a continuous push over time (like a rocket engine). If you want an instantaneous burst of energy, like an explosion or a jump, you should use ForceMode.Impulse. This bypasses the mass calculation to give the object an immediate change in velocity.

By using these methods, you transition from simply observing collisions to actively participating in the physical simulation of your game world.

Key Takeaway: Always apply forces and torques inside FixedUpdate to ensure your physical interactions remain consistent and frame-rate independent.