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

Implementing Dynamic Rotation Using Torque

Learners will apply torque to the Rigidbody to achieve smooth, physics-driven character rotation based on input direction.

Implementing Dynamic Rotation Using Torque

In this lesson, you will learn how to rotate your character naturally by applying physics-based torque rather than directly setting rotation values.

Why Torque?

When you manually change a Transform.rotation or use transform.Rotate(), you are "teleporting" the object to a new angle. This ignores the laws of physics and can cause your character to clip through walls or ignore collision events. By using Torque, you instruct the Rigidbody to apply a rotational force, allowing the physics engine to calculate the character's turn based on its mass and drag. This results in smoother, weightier movement that feels grounded in your game world.

Applying Rotational Force

To apply torque, we use the Rigidbody.AddRelativeTorque method. This method takes a Vector3 representing the axis around which the object should spin. Typically, for a character turning left or right, we apply force around the Y-axis (up).

Here is how you can implement this based on a player's horizontal input:

private Rigidbody rb;
public float torqueAmount = 10f;

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

void FixedUpdate() {
    // Get the horizontal input from our Input Action asset
    float inputDirection = GetHorizontalInput(); 

    // Apply torque around the Y-axis
    // The force is proportional to the input strength
    Vector3 rotationForce = new Vector3(0, inputDirection * torqueAmount, 0);
    rb.AddRelativeTorque(rotationForce, ForceMode.Force);
}

Tips for Success

  1. Use FixedUpdate: Since torque involves the Rigidbody, always apply your forces inside FixedUpdate. This ensures that your rotation calculations remain synchronized with the physics engine's time steps.
  2. Adjust Angular Drag: If your character spins out of control or turns too slowly, check the Angular Drag property on your Rigidbody. Increasing this value acts like "friction" for rotation, helping the character come to a stop once you release the input.
  3. Avoid Conflicts: Ensure you aren't fighting your own movement scripts. If you are using rb.velocity to move forward, torque will naturally cause the character to "bank" or steer while moving, creating a realistic motion arc.

Key Takeaway: Applying torque via AddRelativeTorque allows your character to rotate using physical forces, creating more fluid and immersive movement than traditional transform-based rotation.