Detailed explanation of Unity API - Rigidbody class

The function of the Rigidbody class is to simulate the physical characteristics of the GameObject object in the real world, including gravity, resistance, mass, speed, etc. The code for assigning values ​​to the properties of the RIgidbody object is usually placed in the OnFixedUpdate() method of the script. This blog mainly introduces some instance properties and instance methods of the Rigidbody class, and finally annotates the relationship between APIs with similar functions and strong correlations in the Rigidbody class.

1. Rigidbody class instance attributes

In the Rigidbody class, the instance attributes involved are collisionDectionMode, drag, inertiaTensor, mass, and velocity, which are described below.

1. CollisionDectionMode attribute: collision detection mode

(1) Basic grammar

public CollisionDetectionMode collisionDetectionMode {
    
     get ; set; }

(2) Function description

This property is used to set collision detection for rigid bodies. There are 3 collision detection modes for rigid bodies, that is, three values ​​of the enumeration type collisionDetectionMode.

Discrete: Static discrete detection mode, which is the default setting of the system. In this mode, only when one frame is above one rigid body, and the next frame moves under another rigid body, the crossing phenomenon will occur.

Continuous: Static continuous detection mode, generally used on the target collision body of a high-speed moving rigid body to prevent it from being traversed, and the detection strength is stronger than Discrete

ContinuousDyamic: The strongest continuous dynamic detection mode, generally used on two high-speed moving objects to prevent passing through each other.其计算消耗最大,一般情况下慎用

In short, regardless of the detection mode, it is possible to be traversed. In order to prevent the phenomenon of crossing, in addition to setting the collision detection mode, the thickness of the two-object collider should be appropriately increased. Generally, it is not necessary to make a small fish 0.1, and at the same time reduce the two objects as much as possible. The relative velocity at the time of the collision.

(3) Code implementation

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionDectionMode_test : MonoBehaviour
{
    
    
    public Rigidbody A, B;
    Vector3 v1, v2;

    void Start()
    {
    
    
        A.useGravity = false;
        B.useGravity = false;
        v1 = A.position;
        v2 = B.position;
    }
    void OnGUI()
    {
    
    
        if(GUI.Button(new Rect(10.0f,10.0f,200.0f,45.0f),"Discrete模式不被穿越"))
        {
    
    
            inists();
            A.collisionDetectionMode = CollisionDetectionMode.Discrete;
            B.collisionDetectionMode = CollisionDetectionMode.Discrete;
            A.velocity = new Vector3(0.0f, -10.0f, 0.0f);
        }
        if (GUI.Button(new Rect(10.0f, 60.0f, 200.0f, 45.0f), "Discrete模式被穿越"))
        {
    
    
            inists();
            A.collisionDetectionMode = CollisionDetectionMode.Discrete;
            B.collisionDetectionMode = CollisionDetectionMode.Discrete;
            A.velocity = new Vector3(0.0f, -40.0f, 0.0f);
        }
        if (GUI.Button(new Rect(10.0f, 110.0f, 200.0f, 45.0f), "Continuous模式不被穿越"))
        {
    
    
            inists();
            A.collisionDetectionMode = CollisionDetectionMode.Continuous;
            B.collisionDetectionMode = CollisionDetectionMode.Continuous;
            A.velocity = new Vector3(0.0f, -20.0f, 0.0f);
        }
        if (GUI.Button(new Rect(10.0f, 160.0f, 200.0f, 45.0f), "Continuous模式被穿越"))
        {
    
    
            inists();
            A.collisionDetectionMode = CollisionDetectionMode.Continuous;
            B.collisionDetectionMode = CollisionDetectionMode.Continuous;
            A.velocity = new Vector3(0.0f, -15.0f, 0.0f);
            B.velocity = new Vector3(0.0f, 15.0f, 0.0f);
        }
        if (GUI.Button(new Rect(10.0f, 210.0f, 200.0f, 45.0f), "ContinuousDynamic模式"))
        {
    
    
            inists();
            A.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            B.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            A.velocity = new Vector3(0.0f, -200.0f, 0.0f);
            B.velocity = new Vector3(0.0f, 200.0f, 0.0f);
        }
        if (GUI.Button(new Rect(10.0f, 260.0f, 200.0f, 45.0f), "重置"))
        {
    
    
            inists();
        }
    }
    //初始化A、B
    void inists()
    {
    
    
        A.position = v1;
        A.rotation = Quaternion.identity;
        A.velocity = Vector3.zero;
        A.angularVelocity = Vector3.zero;
        B.position = v2;
        B.rotation = Quaternion.identity;
        B.velocity = Vector3.zero;
        B.angularVelocity = Vector3.zero;
    }
}

In this code, first declare two Rigidbody variables A and B and two Vector3 variables v1 and v2, then set the useGiavity property of A and B to false in the Start method, and assign the Position of A and B to v1 and v2, and then multiple Buttons are defined in the OnGUI method to demonstrate the functions of Discrete, Continuous and ContinuousDynamic. Finally, one inists method is used to reset the state of the rigid body corresponding to the variables A and B.

①Create a new Cube cube, mount the C# script on the cube
insert image description here
②Create a new sub-cube, choose to add a component, click physics; pop up a new window, click rigidbody (rigid body)
insert image description here
③Assign variables A and B to Cube2, Cube3
insert image description here
④View accepts friction and gravity like a real object, and can also collide with other objects
insert image description here

Guess you like

Origin blog.csdn.net/Prototype___/article/details/130602986