Version: Unity 6 Preview (6000.0)
LanguageEnglish
  • C#

Rigidbody2D.totalTorque

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public float totalTorque;

Description

The total amount of torque that has been explicitly applied to this Rigidbody2D since the last physics simulation step.

When adding torque to the Rigidbody2D using Rigidbody2D.AddTorque or Rigidbody2D.AddForceAtPosition (when force is applied away from the worldCenterOfMass) the torque total is summed. When the physics simulation step runs, this total torque is used.

During the next simulation step, the total torque will be time-integrated into the Rigidbody2D.angularVelocity then automatically reset to zero.

NOTE: Only a Rigidbody2D with a Dynamic Body Type will respond to force or torque. Setting this property on a Kinematic Body Type or Static Body Type will have no effect.

using UnityEngine;
using UnityEngine.Assertions;

public class Example : MonoBehaviour { void Start() { // Fetch the rigidbody. var body = GetComponent<Rigidbody2D>();

// Make the assumption the body has no previous torque applied. Assert.AreApproximatelyEqual(0.0f, body.totalTorque, Mathf.Epsilon);

// Initialize a torque. var torque = 5f;

// Add the torque. body.AddTorque(torque);

// The total torque should be what we just added. Assert.AreApproximatelyEqual(torque, body.totalTorque, Mathf.Epsilon);

// Add the same torque again. body.AddTorque(torque);

// The total torque should still be what we've added. Assert.AreEqual(torque * 2f, body.totalTorque);

// We can reset any torque that has been applied since the last simulation step. body.totalTorque = 0f; } }