“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The rigid body (RigidBody)

In Unity, RigidBody is used to simulate this physics effect. When a gameobject is assigned a RigidBody component, the game engine calculates and simulates its physics effect. We can also apply various forces to this object to make it move. In addition, to achieve the effect of gravity, the corresponding game object must have rigid body components attached.

Add components: In the Inspector panel ->Add Component->Physics->Rigidbody

Properties on the component panel

  • Mass: Mass. The greater the mass, the greater the inertia. It is recommended that the mass of objects in the scene should not differ by more than 100 times. Prevent collision of two objects of too different mass from causing too much speed, which affects the performance of the game and the effect of the presentation.

  • -Leonard: Drag? In this case, air resistance. The value of the property affects the speed effect that impedes the straight-line motion of the object. When a force is applied to a game object, the higher the value, the harder it is to move. If set to infinite, the object will stop moving immediately.

  • Angular Drag: Angular Drag. It also refers to air resistance, but it’s used to prevent things from spinning. If set to infinite, the object will stop spinning immediately.

  • If you do not select this option, Gravity will not affect the system.

  • Kinematic: whether or not Kinematic (whether or not driven by physical engines). If checked, it will no longer be affected by the physics engine, but will be affected by the Transform. That is, there will be no gravity, no collision, etc., and it will only stay in the position specified by the Transform. When the object hits, it will not fall down like a wall, and its position will not change due to the collision.

  • Interpolate: Difference type. If you see that the rigid body is not moving smoothly, you can choose a smooth mode. That is: smooth object motion curve. None: Difference smoothing is not used. Interpolate: Smoothing movement based on a previous frame. Extrapolate: Smooth movement by Extrapolate where the object will be next frame.

  • Collision Detection. It is used to change the accuracy of object collision detection. Discrete: default collision detection mode. However, if object A moves very fast, it is possible that the previous frame is still in front of object B, and the next frame is behind object B. In this case, collision events will not be triggered. Therefore, if such A situation needs to be detected, the latter two detection methods must be used. Continuous: This allows collision detection with game objects that have static grid colliders. It can avoid the situation where one object moves too fast and passes through another. Continuous Dynamic: This allows collision detection with all game objects with mode 2 or 3 set.

  • Constraints: Constraints. Constrain the x/ Y /z coordinates of the position or rotation to Freeze. For example, if you want your game object to walk up stairs without falling, or to hit a wall object at high speed without spinning around, freeze the rotation of the X, Y, and Z axes.

  • CenterOfMass: the centerOfMass relative to the transformation origin.

  • AngularVelocity the angularVelocity vector of a rigid body that can be modified to rotate the rigid body

Note: When dealing with Rigidbody, physical simulations generally require FixedUpdate instead of Update.

Add force to code:

  • AddForce Specifies the force added to the rigidbody.
  • AddForceAtPosition applies force to position. As a result this will apply a torque and a force to the object.
  • AddRelativeForce adds force to the rigidbody. With respect to its system coordinates.

This section describes the Constant Force component properties

(Attached to rigid body components)

  • Force: Absolute Force world coordinate Force.
  • The Relative Force of the Relative Force.
  • Torque[tɔ:k] : Torque world coordinates Torque.
  • Relative Torque: Relative Torque of its own coordinate.

Correlation function:

  • Rigid-body Sleep and WakeUp functions
  • Sleep causes the rigid body to Sleep without moving.
  • WakeUp enables the rigid body to move.

Movement is controlled by the velocity vector of the rigid body

The sample

Create a Cube, add Rigidbody components, and write a script that uses Rigidbody. Velocity to drive the object up by pressing the space bar.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	private Rigidbody rigidbody;
        
	void Start()
        {
		 // Get the RigidBody component on the mounted object
		 this.rigidbody=this.GetComponent<RigidBody>();
	}
        
	void FixedUpdate()
        {
		if (Input.GetButtonDown("Jump"))  // Press space
                {
		  	// Set the velocity vector for the rigid body
			this.rigidbody.velocity = new Vector3(0.10.0); }}}Copy the code

AddExplosionForce applies a force to a rigid body to simulate the effect of an explosion.

Instance in API documentation

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float radius = 5.0 F;
    public float power = 10.0 F;

    void Start() 
    {
        Vector3 explosionPos = transform.position;
        Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
        foreach (Collider hit in colliders)
        {
            Rigidbody rb = hit.GetComponent<Rigidbody>();

            if(rb ! =null)
                rb.AddExplosionForce(power, explosionPos, radius, 3.0 F); }}}Copy the code