[Unity Physics] Physics-rigidbody, Collider

Source: Internet
Author: User

What is Collider

The most basic condition in which the collider component triggers a physical collision in the unity engine.

It can be said that it is impossible to have a physical collision system in a game.

What is Rigidbody

The position of the control object through the physical simulation.

The Rigidbody component adds a unified physics engine to an object under the control of its motion. Even without adding any code, the Rigidbody object will pull down by gravity and will react with the incoming object if the correct Collider collision component also exists.

Rigidbody also has a scripting API that allows you to control it in a way that is applied to objects and physical reality. For example, the behavior of a car can be specified in terms of power applied to the wheel. Given this information, the physics engine can handle most of the other aspects of car movement, so it will speed up the actual and correct response to conflicts.

The Rigidbody setting is usually changed in the Fixedupdate function of the script. The reason is to prevent the time when the update is not matched with frame update steps.

Fixedupdate is called every physics before it is updated immediately, so any changes will be handled directly.

The common problem with getting started with rigidbody is that the physical systems that are simulated in the game appear to be running "slow motion". This is actually due to the scale used for your model. The default gravity setting assumes that a world unit corresponds to a distance of one metre. Unlike non-realistic games, there is no big difference if your models are 100 units long but when using physics, they will be considered very large objects. If mass-used objects should be small, they seem to be very slow--the physics engine thinks they are very large objects fall very large distances. With this in mind, be sure to keep your object in real life more or less the size (so the car should be about 4 units = 4 meters).

The use of Collider

common methods of colliding device
    • Oncollisionenter
// This method is called when a rigid body or collider of another game object collides with the collider component of the game object ... Collider.oncollisionenter (collision)

For example:

void Oncollisionenter (Collision collision) {        //Traverse all objects        that collide with the collider component of the game object ... foreach (ContactPoint contact in collision.contacts) {            Debug.drawray (contact.point, Contact.normal, Color.White);        }       }
    • Oncollisionexit
This method is called when another game object has left the rigid body of the current game object or is a collider build ... Collider.oncollisionexit (collision)
    • Oncollisionstay
This method is called when the collider of another game object has been stuck to a rigid body or collider on the current game object ... Collider.oncollisionstay (collision)

Note: When the trigger property of the collider Collisider component is set to True, the following three methods are called in the script

    • Ontriggerenter
It is important to note that the parameters of the trigger callback method are Collider type Collider.ontriggerenter (Collider)
    • Ontriggerexit
Collider.ontriggerexit (Collider)
    • Ontriggerstay
Collider.ontriggerenter (Collider)

the use of Rigidbody
member variables for rigidbody
Member Variable name Role of member variables
Angulardrag Angular resistance for game objects
Angularvelocity Vector of angular velocity of the game object
Centerofmass The centroid position of the coordinate system relative to the game object itself
Collisiondetectionmode Collision Detection mode for the body of a game object
Constraints Used to constrain a game object to be free to move with Unity's physical system on certain conditions
Detectcollisions Whether to enable collision detection for the game object. The default is open.
Drag The resistance of a game object in motion
Freezerotation Used to constrain whether the game object will be rotated by the unity physical system
Iskinematic Whether the rigid body used to control the game object receives the impact of Unity's physical system
Mass Used to represent the quality of a body of a game object
Maxangularvelocity Used to indicate the maximum angular speed of a game object
Position The position coordinates of the body of the game object rigid component
Rotation The coordinate rotation of the object's rigid body component has been generated
Sleepangularvelocity Used to indicate that a game object stops when its angular velocity is less than the critical value. Default value 0.14
Sleepvelocity Used to indicate that a game object stops when it is running at a lower speed than the critical value. Default value 0.14
Usegravity Used to indicate whether the current game object is affected by the gravity system
Velocity The speed of the rigid body used to represent the current game object
Worldcenterofmass The centroid position of the coordinate system relative to the world
component interface panels used in the Unity development tool

common methods of Rigidbody
    • Addforce
Adds a directional force to the rigidbody. Therefore, the rigidbody will begin to move. void Addforce (Vector3 force, Forcemode mode = Forcemode.force);

For example:

In the Fixedupdate function, add 10 units upward of the direction Force void Fixedupdate () {rigidbody for rigidbody        . Addforce (Vector3.up * 10);}

    • Addforceatposition
Add a force to rigidbody in one place. void Addforceatposition (Vector3 forces, Vector3 position, Forcemode mode = Forcemode.force);

For example:

The Applyforce function is a custom function used to exert a directional force on a rigid body object void Applyforce (Rigidbody body) {        //calculates the orientation        of the current game object and the body parameter's position in the game object Vector3 direction = body.transform.position-transform.position;        In the position of the current game object, apply 1 units of direction force body to the body parameter        . Addforceatposition (direction.normalized, transform.position);}    

    • Addtorque
Adds a moment to the rigid body component that causes the game object to rotate faster ... void Addtorque (Vector3 torque, Forcemode mode = Forcemode.force);

For example:

Add a 10-unit rotational force to the game object ... void Fixedupdate () {        rigidbody. Addtorque (Vector3.up * 10);}

The resulting effect is as follows

    • Addrelativetorque
Add a torque rigidbody relative to Rigidbody's own coordinate system void Addrelativetorque (Vector3 torque, Forcemode mode = Forcemode.force);

For example:

With respect to the coordinate system of the game object itself, add 10 units of force void Fixedupdate () {rigidbody along the y-axis forward rotation. Addrelativetorque (Vector3.up * 10);}

So there are two common components in the Unity Physics engine system, collider and Rigidbody.

I hope you have a lot of support.

[Unity Physics] Physics-rigidbody, Collider

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.