Detailed explanation of Unity using AddTorque method to apply torque to rigid body

To apply force to a rigid body, in addition to using the AddForce method, we can also use the AddTorque method. This method applies a force to a rigid body by applying a moment. The AddTorque method is similar in form to AddForce. It also has 4 overloaded methods:

1. AddTorque(Vector3 torque); uses Vector3 type parameters to indicate the magnitude and direction of the torque. The torque is applied in the local coordinate system of the rigid body. In the following case rb uniformly uses the variable private Rigidbody rb;

Vector3 torque = new Vector3(0f, 10f, 0f);
rb.AddTorque(torque);

2. AddTorque(float x, float y, float z); uses three float type parameters to represent the magnitude of the moment on the x, y, and z axes respectively. The moment is applied in the local coordinate system of the rigid body.

rb.AddTorque(0f, 10f, 0f);

In fact, the above two methods are equivalent. Just the parameter types used are different.

3. AddTorque(Vector3 torque, ForceMode mode); among them, the parameter torque represents the magnitude and direction of the torque, and the parameter mode represents the mode of action of the torque. The modes are:

  • ForceMode.Force: Apply a continuous torque.
  • ForceMode.Impulse: Apply an instantaneous torque.
  • ForceMode.Acceleration: Apply a continuous acceleration moment.
  • ForceMode.VelocityChange:Apply a moment that changes the velocity of the rigid body.
Vector3 torque = new Vector3(0f, 10f, 0f);
rb.AddTorque(torque, ForceMode.Impulse);

 4. AddTorque(float x, float y, float z, ForceMode mode); among them, the parameters, x, y, z represent the magnitude of the torque on the x, y, z axis. The parameter mode is an enumeration type parameter used to specify the action mode of the torque. The modes are:

  • ForceMode.Force: Apply a continuous torque.
  • ForceMode.Impulse: Apply an instantaneous torque.
  • ForceMode.Acceleration: Apply a continuous acceleration moment.
  • ForceMode.VelocityChange:Apply a moment that changes the velocity of the rigid body.
rb.AddTorque(0f, 10f, 0f, ForceMode.Impulse);

 In fact, the two overloaded methods 3 and 4 are equivalent, but the torque parameter types are different.

Although AddForce and AddTorque seem to have similar methods, they still have essential differences.

  • First, the AddForce method applies force to the rigid body and changes the linear motion state of the rigid body, that is, position and speed, while the AddTorque method applies a moment and changes the angle and angular velocity of the rigid body.
  • Secondly, their action points are different. The AddForce method acts on the center of mass of the rigid body, that is, the center point, while AddForce scopes its center of rotation.

On the window, we can see that the difference between the changes in their forces is: one directly changes the value of Position, while the other's first function is to change Rotation, thereby driving changes in Position.

Guess you like

Origin blog.csdn.net/mr_five55/article/details/135030116