Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


AddTorque(float, ForceMode2D)

Apply a torque at the rigidbody's centre of mass.
Read time 1 minuteLast updated 13 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule
public void AddTorque(float torque, ForceMode2D mode)

Parameters

torque

Torque to apply.

The force mode to use.

Remarks

Applying torque to the Rigidbody2D changes the _angularVelocity only. This change is scaled (divided) by the rotational _inertia. Therefore, a larger _inertia results in smaller changes to _angularVelocity, and a smaller _inertia results in larger changes to _angularVelocity.
When applying torque either as a force or an impulse, you can use any value to get the required change in _angularVelocity. However, if you require a specific change in degrees, then you must first convert the
torque
value into radians by multiplying with Mathf.Deg2Rad then multiplying by the _inertia.
The following example demonstrates this as an impulse:

Examples

using UnityEngine;public class TorqueRotationExample : MonoBehaviour{ // Add an impulse which produces a change in angular velocity (specified in degrees). public void AddTorqueImpulse(float angularChangeInDegrees) { var body = GetComponent<Rigidbody2D>(); var impulse = (angularChangeInDegrees * Mathf.Deg2Rad) * body.inertia; body.AddTorque(impulse, ForceMode2D.Impulse); }}