This is the 19th day of my participation in the August More Text Challenge

concept

Once you add the Rigidbody component to an object, its motion is controlled by the Unity physics engine. Even without adding any code, the Rigidbody object experiences downward gravity and reacts when colliding with other objects (if the appropriate Collider component is also present).

Rigidbody also has a scripting API that lets you apply force to objects and control them with realistic physics. For example, the behavior of a car can be specified based on the force exerted by the wheels. Based on this information, the physics engine can handle most other aspects of the car’s motion so that the car accelerates realistically and responds appropriately to a collision.


Public methods

Public function explain
AddExplosionForce Apply force to a rigid body that simulates the effect of an explosion.
AddForce Add force to Rigidbody.
AddForceAtPosition Apply force at position. This applies torque and force to the object.
AddRelativeForce Adds force to a rigid body (with respect to its coordinate system).
AddRelativeTorque Adds torque to the rigid body (relative to its coordinate system).
AddTorque Add torque to rigid body.
ClosestPointOnBounds The point closest to the bounding box of the attached collider.
GetPointVelocity The velocity of the rigid body at worldPoint (global space).
GetRelativePointVelocity The velocity relative to the rigid body at the point relativePoint.
IsSleeping Whether the rigidbody is asleep or not
MovePosition Move the rigidbody to Position.
MoveRotation Rotate the rigid body into rotation.
ResetCenterOfMass Resets the center of mass of the rigid body.
ResetInertiaTensor Resets the value and rotation of the inertia tensor.
SetDensity Set the mass according to the additional collider (assuming constant density).
Sleep Force the rigidbody to sleep for at least one frame.
SweepTest Tests whether a rigid body collides with any objects as it moves through the scene.
SweepTestAll Similar to rigidBody.sweeptest, but returns all hit objects.
WakeUp Force wake up rigid body.

Code interpretation

Four modes:

  1. Forcemode. Force: Gives an object a sustained Force
  2. Acceleration: Gives the object a constant Acceleration, but ignores its mass, which defaults to 1
  3. ForceMode.Impulse: To add a momentary force to an object
  4. Forcemode. VelocityChange: Add an instant acceleration to an object, ignoring its mass

Function:

  • Add a force to the rigid body, referring to world coordinates
AddForce (force : Vector3, mode : ForceMode = ForceMode.Force)
Copy the code
  • Add a force to the rigid body, referring to the local coordinates
AddRelativeForce (force : Vector3, mode : ForceMode = ForceMode.Force) 
Copy the code
  • Add a torque to the rigid body, referring to world coordinates
AddTorque (torque : Vector3, mode : ForceMode = ForceMode.Force)
Copy the code
  • Add a torque to the rigidbody, referring to the local coordinates
AddRelativeTorque (torque : Vector3, mode : ForceMode = ForceMode.Force)
Copy the code
  • Note That under the action of resistance, the object will slowly stop and apply force at position. As a result this will apply a torque and force (instantaneous) to the object.
AddForceAtPosition (force : Vector3, position : Vector3, mode : ForceMode = ForceMode.Force)
Copy the code

Code sample

using UnityEngine;

public class RigidbodyTest : MonoBehaviour
{
    public Rigidbody rb;

    void Start()
    {
        // Get the rigidbody component
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // Keep adding force
        rb.AddForce(transform.forward * 10); }}Copy the code