Unity API Parsing (9)--rigidbody class

Source: Internet
Author: User

Simulating the physical characteristics of Gameobject objects in the real world (gravity, resistance, mass, speed)

Assignment codes for Rigidbody object properties are usually placed in the Onfixedupdate method of the script

Collisondetectionmode Properties--Collision detection mode

There are 3 types of collision detection modes for rigid bodies

discrete--Static discrete detection mode

continuous--static continuous monitoring mode, generally used in high-speed moving rigid body of the target collider

continousdynamic--the strongest continuous dynamic detection mode

Drag properties--rigid body resistance

Drag higher values the faster the rigid body slows down, when drag >0, the rigid body increases to a certain speed uniform motion

The maximum velocity value of a rigid body in free fall is only related to gravity and drag values, not mass

Inertiatensor Properties--inertia tensor

At the same distance from the center of gravity, just feel inclined to the large side of the tensor value

Rigidbody.inertiatensor = new Vector3 (5.0f,10.0f,1.0f)

Using unityengine;using system.collections;public class inertiatensor_ts:monobehaviour{void Ongui () {if ( Gui. button (new Rect (10.0f, 10.0f, 160.0f, 45.0f), "x-axis inertia tensor greater than y-axis")) {transform.position = new Vector3 (0, 4, 0)            ;            Transform rotation around the z-axis 45 degrees Transform.rotation = Quaternion.euler (0.0f, 0.0f, 45.0f); Set the inertia tensor of the rigidbody//x axis component value is greater than the y-axis, then just realize tilt to the x-axis rigidbody.inertiatensor = new Vector3 (15.0f, 10.0f, 1.0f)        ; } if (GUI. button (new Rect (10.0f, 60.0f, 160.0f, 45.0f), "y-axis inertia tensor greater than x-axis")) {transform.position = new Vector3 (0, 4, 0)            ;            Transform.rotation = Quaternion.euler (0.0f, 0.0f, 45.0f);         Set the inertia tensor of the rigidbody//x axis component value is less than the y-axis, then just realize the direction of the y-axis rigidbody.inertiatensor = new Vector3 (5.0f, 10.0f, 1.0f); } if (GUI. button (new Rect (10.0f, 110.0f, 160.0f, 45.0f), "x-axis and y-axis inertia tensor")) {transform.position = new Vector3 (0, 4,            0); TraNsform.rotation = Quaternion.euler (0.0f, 0.0f, 45.0f);         Set the inertia tensor of the rigidbody//x the same as the y-axis inertia tensor, then just realize to remain still rigidbody.inertiatensor = new Vector3 (10.0f, 10.0f, 1.0f); }    }}

Mass properties--rigid body mass

General rigid body mass value in the vicinity of 0.1 simulation best, the maximum not more than 10, otherwise prone to simulation instability

The main function of mass is to calculate the velocity of an object after a collision.

Velocity Property--rigid body velocity

The direction of V is relative to the world coordinate system, regardless of whether the body is given a velocity vector of type Vector3 or the speed of the current rigid body.

Velocity is measured in meters per second-meters are the default length units in Unity

Addexplosionforce Method--simulating explosive force

The size of the explosive force, the coordinates of the explosive point, the effective radius of the explosive force

addforceatposition--increases the force of the rigid point

Add one force to the parameter position point

The Addforce method does not generate torque when the force is applied to a rigid body to rotate the object, but this method

Using unityengine;using System.collections;public class addexplosionforce_ts:monobehaviour{public Rigidbody A;    Public Transform Z;//scene View shows explosion point coordinates Vector3 E = vector3.zero;//explode point coordinates float F, R, Y_m;    BOOL Is_change = false; void Start () {///initial position equals the x, y axis coordinate values of the explosion point and a//can change the size of F and R view run-time results E = a.position-new Vector3 (0.0f, 0.0f        , 3.0f);        F = 40.0f;        R = 10.0f;        Y_m = 0.0f;        A.transform.localscale = Vector3.one * 2.0f;    Z.position = E;            } void Fixedupdate () {if (Is_change) {A.addexplosionforce (F, E, R, y_m);        Is_change = false; }} void Ongui () {//When the explosion point is equal to two axes of the center of gravity of A, a will pan without rotating if (GUI.            button (new Rect (10.0f, 10.0f, 200.0f, 45.0f), "rigid body movement not rotated") {Is_change = true;        Inits (); }//Although the force size is constant, the resulting torque occurs when rotating if (GUI.           button (new Rect (10.0f, 60.0f, 200.0f, 45.0f), "the rigid body moves but does not change the Force size")) {inits (); A.position + = new Vector3 (0.5f, -0.5f, 0.0f);        Is_change = true; } if (GUI.            button (new Rect (10.0f, 110.0f, 200.0f, 45.0f), "The size of the force calculated by the nearest surface distance")) {inits ();            A.position + = new Vector3 (0.0f, 2.0f, 0.0f);        Is_change = true; The offset of the//y axis changes the original direction of a//can change the value of y_m to see different effects if (GUI. button (new Rect (10.0f, 160.0f, 200.0f, 45.0f), "Y?á ¢éú??"        Ò? "))            {inits ();            Is_change = true;            A.position + = new Vector3 (0.0f, 2.0f, 0.0f);        Y_m = -2.0f;        }}//Initialize data void Inits () {a.velocity = Vector3.zero;        a.angularvelocity = Vector3.zero;        A.position = E + new Vector3 (0.0f, 0.0f, 3.0f);        A.transform.rotation = quaternion.identity;    Y_m = 0.0f; }}

Addtorque Method--rigid body adding torque

Using unityengine;using System.collections;public class Addtorque_ts:monobehaviour {public    rigidbody R;    Vector3 m_torque = new Vector3 (0.0f,10.0f,0.0f); void Start () {        R.transform.localscale = new Vector3 (2.0f,2.0f,2.0f) ;        R.mass = 1.0f;        R.angulardrag = 0.0f;        Debug.Log ("Maximum angular velocity of the rigid body default" +r.maxangularvelocity);        You can change the maximum angular velocity of a rigid body using the following code        //r.maxangularvelocity = 10.0f;} void Fixedupdate () {        //each frame adds a torque to the object, increasing its rotational speed        r.addtorque (m_torque,forcemode.force);        Debug.Log ("The current angular velocity of the rigid body" +r.angularvelocity);}}

Closestpointonbounds Method--the shortest distance from explosion point to rigid body

This method is typically used to calculate the size of the explosive force in Addexplosionforce

getpointvelocity--Rigid Point Speed

Obtain the velocity of Worldpoint point in the local coordinate system of the rigid body in the world coordinate system, and the velocity calculation is affected by the angular velocity of the rigid body.

getrelativepointvelocity--rigid point relative velocity

moveposition--Rigid body Position movement

Moving the position of a rigid body, usually when--iskinematic is true when a rigid body loses its kinetic simulation

sleep--Rigid Body Sleep

sweeptest--Detecting Collider

Detects if there is a collider object in the direction direction of the rigid body, and the effective detection distance of the object is not greater than distance

Using unityengine;using System.collections;public class sweeptest_ts:monobehaviour{public gameobject A, B;    Raycasthit hit;        Float len = 10.0f;//effective Detection distance void Start () {a.transform.position = new Vector3 (1.0f, 1.0f, 1.0f);    B.transform.position = new Vector3 (4.0f, 1.0f, 1.0f); } void Fixedupdate () {//detects if an object exists within the right Len distance of the rigid body A (A.rigidbody.sweeptest (A.transform.right, out hit, le        N) {Debug.Log ("A object exists on the right of object" + Hit.transform.name + "Its distance from A is" + hit.distance);        } else {Debug.Log ("a object to the right" + len + "meter range does not detect an object with a collider"); }} void Ongui () {if (GUI.            button (new Rect (10.0f, 10.0f, 200.0f, 45.0f), "set B coordinate so that a cannot detect")) {//reset B position so that the spacing of the object, a, is greater than the Len value        B.transform.position = new Vector3 (12.0f, 1.0f, 1.0f); } if (GUI.  button (new Rect (10.0f, 60.0f, 200.0f, 45.0f), "Cancel the Rigidbody component in B")) {//Destroy the Rigidbody component in the B object// Run the program toIt is found that the presence of rigidbody in the B object has no effect on the detection result if (b.getcomponent<rigidbody> ()) {Destroy (b.getc            Omponent<rigidbody> ()); }} if (GUI. button (new Rect (10.0f, 110.0f, 200.0f, 45.0f), "Cancel the collider component in B")) {//Destroy the collider component in the B object// The running program finds that if no Collider component in B is in any case, a does not detect the presence of B if (b.getcomponent<collider> ()) {Destroy            (B.getcomponent<collider> ()); }}//Resets the state of the B object to the IF (GUI. button (new Rect (10.0f, 160.0f, 200.0f, 45.0f), "reset")) {b.transform.position = new Vector3 (4.0f, 1.0f, 1            .0f); if (!            B.getcomponent<collider> ()) {b.addcomponent<boxcollider> (); } if (!                B.getcomponent<rigidbody> ()) {b.addcomponent<rigidbody> ();            B.rigidbody.usegravity = false; }        }    }}

sweeptestall--Collision Detection Device

Detects if there is a collider within the distance distance of the direction direction of the rigid body and returns the raycasthit of all detected objects

Using unityengine;using System.collections;public class sweeptestall_ts:monobehaviour{public    gameobject A, B, C, D ;    Raycasthit[] Hits;    Float len = 10.0f;//effective detection distance    void Start ()    {        a.transform.position = new Vector3 (1.0f, 1.0f, 1.0f);        B.transform.position = new Vector3 (4.0f, 1.0f, 1.0f);        C.transform.position = new Vector3 (7.0f, 1.0f, 1.0f);        The D object is outside the effective detection range of a and will not be detected by        d.transform.position = new Vector3 (12.0f, 1.0f, 1.0f);        hits = A.rigidbody.sweeptestall (A.transform.right, Len);        float L = hits. Length;        Debug.Log ("A detected number of objects" + L + "they are respectively");        Traverse        foreach (Raycasthit hit in hits)        {            Debug.Log (hit.transform.name);}}    }

WakeUp Method--Awakening rigid body

Automatic wake-up of other rigid bodies colliding with rigid bodies in hibernation, moving rigid bodies with joint links, changing rigid body properties, exerting a force on the dormant rigid body

Usegravity used to determine whether a rigid body accepts the gravitational acceleration

Iskinematic attribute--used to determine whether to accept dynamic simulations (gravity sensing, speed, resistance, mass)

The value of the rigid body velocity is only related to gravity, drag and kinematic, and is independent of the mass mass and the sacle of the object.

If the density of a rigid body is not set using the Rigidbody.setdensity method in the script, the mass mass value of the rigid body is the size mass in the Inspector panel, which is independent of the scale of mass, otherwise

Two objects collide with momentum

function annotation of force mode Forcemode

forcemode.force--use rigid body mass calculation to calculate momentum per frame interval

Forcemode.acceleration

Forcemode.impulse

Forcemode.velocitychange

Ontriggerxxx Oncollisionxx

If there is no rigidbody component in a, B will pass through the B object regardless of whether it contains rigidbody components, and the Ontriger and oncollision in A and B scripts will not be called

To activate the above two methods, you must make the moving object contain the Rigidbody component

Unity API Parsing (9)--rigidbody class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.