[Unity] Collider, Trigger, Rigidbody, Dynamic, Kinematic, Static, OnCollision, OnTrigger Full Lecture

Explain in detail about various details of collision in Unity, the article starts with Unity2D, and attaches the relevant introduction about Unity3D

The article is updated for a long time, and the comment area is also welcome to correct or supplement
. In addition, if you encounter some problems, it is recommended to read the entire article. There are some common minor problems at the end

Collider / Collider

The collider is the most basic thing used to detect collisions. For example, if you want to do physical effects, you need a wall, throw a ball over it, and the wall blocks the ball. In this process, you need to judge whether the wall collides with the ball

There are many types of colliders, such as BoxCollider, CircleCollider, etc., but the purpose is the same, but the shape of the detection range is different.

Trigger / Trigger

There is an IsTrigger property in the collider, which can set whether the current collider is a "trigger". If true is set, that is, when a collider is a trigger, then this object will no longer participate in any physical collision.

For example, if the collider of a wall is set to IsTrigger, then the wall can no longer block a ball.

Although the game object with the trigger set does not participate in physical collisions, it still has physical effects, for example, you can set its speed, weight, etc., these are still valid

Rigidbody / RigidBody

Rigid body is the most basic component to achieve physical effects in Unity. For example, a game object with a rigid body can be affected by gravity. When two game objects with a rigid body collide again, they will "block" as shown, instead of passing through each other.

The rigid body depends on the collider. When you add a rigid body component, it will also add a collider by default. If you need other colliders, you can manually change it yourself

There is the most important option "Body Type" in the rigid body, and its value can be dynamic (Dynamic), kinematic (Kinematic), static (Static)

When introducing various types of rigid bodies below, "Whether a collision occurs" indicates whether the "OnCollision" series of events will be triggered. "Affected" indicates whether the rigid body will be affected by the force of the target rigid body.

For example, Dynamic will be blocked by Kinematic rigid body, and Dynamic can also be knocked away by Kinematic, but Kinematic will not be affected by Dynamic rigid body, so Kinematic cannot be knocked away by Dynamic

In addition, if the rigid body A will not pass through the rigid body B, it means that the rigid body B has an influence on A, that is, when the rigid body A is stationary, the rigid body B rushes to A at a certain speed, then it is possible

There are no Static Rigidbodies in Unity 3D.

Dynamic / Dynamic

When the Body Type of a rigid body is Dynamic, this game object has all the physical effects, including gravity, and the force between game objects. It will have physical collision effects with all other rigid bodies, and can trigger "OnCollision " series of events.

When the current game object is a dynamic rigid body:

Another Object Rigidbody Type Is there a collision Affected reason
Dynamic O O Dynamic rigid bodies are affected by all rigid bodies and have all physical properties
Kinematic O O ditto
Static O O ditto

In Unity 3D, the default behavior of a rigid body is Dynamic

Kinematic / Kinematic

When the Body Type of a rigid body is Kinematic, the game object only has a part of the physical effect, and consumes less performance than a dynamic rigid body.

It will not be affected by gravity or any other force, you can understand that it has infinite mass, and when any other game object hits it, it will not move its position.

Moreover, there will be no physical collision with other Kinematic rigid bodies, because Kinematic itself is not designed for collision, it is more suitable for walls or game objects that need to be manually operated by scripts, and of course it will not trigger the "OnCollision" series of events.

When the current game object is a kinematic rigid body:

Another Object Rigidbody Type Is there a collision Affected reason
Dynamic X X Kinematic will only run rampant, velocity, angular momentum will not be affected in any way (refer to the water droplets in the three-body)
Kinematic O X ditto
Static O X ditto

In Unity 3D, check the IsKinematic option to make the current rigid body a kinematic rigid body

Static / Static

A static rigid body should not be moved, or it has very little use, because it will only collide with a Kinematic rigid body. In the game, it should be used as a wall, or a ground.

When the current game object is a static rigid body:

Another Object Rigidbody Type Is there a collision Affected reason
Dynamic X X Can your little basketball knock the earth into the air?
Kinematic O X Static rigid bodies can only collide with Dynamic 2D rigid bodies
Static X X Because this rigid body is not designed to move

Collision Events / OnCollision

When two rigid bodies collide, they can generate collision events. The specific objects that can generate collision events have been mentioned above.

It should be noted that once a collision event occurs, both parties will trigger the Unity message of OnCollision. And the parameters of Collision passed in are also different. But no matter what, the collider property of Collision is the same as the collider instance of the current collision (otherCollider property Generally rarely used)

Collision events have the following

method name describe
OnCollisionEnter / OnCollisionEnter2D OnCollisionEnter is called when this collider/rigidbody has come into contact with another rigidbody/collider
OnCollisionStay / OnCollisionStay2D OnCollisionStay is called once per frame for each collider or rigidbody that touches another collider or rigidbody
OnCollisionExit / OnCollisionExit2D OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider

Trigger event / OnTrigger

To use a trigger, and to trigger a trigger event, it must be ensured that: both colliding objects contain colliders, and at least one of the colliders sets IsTrigger to true, and at least one of them must contain a non-static (Static) rigid body

The trigger events are as follows

method name describe The author's vernacular
OnTriggerEnter / OnTriggerEnter2D When a GameObject collides with another GameObject, Unity calls OnTriggerEnter Called when two game objects start to collide
OnTriggerStay / OnTriggerStay2D Called every frame when another object is within the trigger collider attached to this object As long as the two colliders are still in the state of collision, it will be called once every frame
OnTriggerExit / OnTriggerExit2D Called when another object leaves a trigger collider attached to the current object Call when the two game objects no longer collide passionately

As mentioned on the Internet, both objects have colliders, and one of them is a rigid body, because there is no static rigid body in Unity3D, so as long as there is a rigid body, it can be triggered. In 2D, the static rigid body does not trigger the trigger event.

label

  1. 3D events and 2D events are not universal. If you are a 2D game object and use 2D colliders and rigid bodies, then when using OnCollision and OnTrigger methods, you must use methods with 2D suffixes
  2. It should be noted that, for example, a ground has only one collider and does not contain any rigid body, a game object with a collider and a rigid body can stand on this ground, and can trigger collision-related events normally.
  3. If you are doing a ground stab effect, the player will continue to receive damage while standing, but OnTriggerStay may not be triggered all the time. By default, it can be triggered continuously after 0.5s of Enter, and then enters the "Sleep" state. At this time, unless the player moves, the The TriggerStay message will be triggered again. If you don't want it to go to sleep, then change the Sleep mode of the corresponding rigid body to Never sleep. If you want to change this time globally, you can set Time in Physics2D in Project Settings to Sleep to the value you want.

For any wrong information or deficiencies in this article, please correct me in the comment area

Guess you like

Origin blog.csdn.net/m0_46555380/article/details/127067629