Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Colliders and triggers

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. What is a collider

Collider is a group of components, which includes many kinds, such as Box Collider Box Collider, Capsule Collider capsule Collider, Mesh Collider Grid Collider, Sphere Collider Ball Collider and so on. These colliders are used in different situations, but they must be added to GameObjecet. (as shown below):

2. What is a trigger

The so-called trigger only needs to check the IsTrigger property selection box in the Collider component in the inspector panel. As shown below:

3. Difference:

Collider

1) Collider has collision effect

2) IsTrigger=false

3) callback function of collision information detection

1.OnCollisionEnter (Collision other) when entering the Collider (when starting the collision)

2.OnCollisionStay (Collision other) when staying with the Collider (when colliding all the time)

3.OnCollisionExit (Collision other) when exiting the Collider (when ending the collision)

Trigger

1) the trigger has no collision effect.

2) IsTrigger=true

3) trigger information detection:

1.OnTriggerEnter (Collider other) when entering the trigger

2.OnTriggerExit (Collider other) when exiting trigger

3.OnTriggerStay (Collider other) as a stay trigger

4. Necessary conditions for collision and trigger

Collision test

1) collider is added to both cube, and penetration occurs when the game is running

2) We add rigidbody for × × cube and cancel the gravity option on rigidbody.

When the game was running, we found that × × × cube was bounced off, while the red cube did not change.

3) We also add rigidbody to the red cube to cancel the gravity option

When the game is running, both cube are bounced off.

Collision summary

1. If two objects, as long as one of them has no Collider, even if any one of them has rigidbody, it will traverse (that is, crossing without Collider)

2. If both objects have Collider, the object with rigidbody will collide, while the party without rigidbody will not be affected (pay attention to who touches whom)

3. If both objects have Collider, and objects without rigidbody touch objects with rigidbody, there will be no collision.

(easy to ignore, take it out alone. In addition, when moving objects without rigidbody at high speed, collisions occasionally occur. I wonder if it is a bug of unity).

To sum up, the necessary conditions for two objects to collide are:

Both objects must have a Collider, at least one of which has a Rigidbody rigid body. (only the side with the Rigidbody rigid body will be affected by the physics engine)

Some people may think that if you add rigid bodies to all objects, you don't have to be so tangled.

There are two reasons:

First, there are a lot of objects in the game scene, all with rigid bodies, so the system will cost a lot of money.

Second, sometimes I need one of the objects to be motionless, such as the ground.

Note:

1. In a collision, "just contact" and "contact finish" are called only once, while "in collision" is called all the time during contact.

2. The parameter of the function is the object that was hit. We can get the information of the hit object through this parameter.

Let's test the callback function:

Let's first create a scene like this.

We add rigidbody to the red ball, check the gravity option, and add a script

The following figure shows the content of the script:

Run the game and the output is as follows:

From the above test, we can clearly see the timing of calling the three methods.

Where the other parameter is the collided object

Trigger test

1. Let's do an example of collision testing. Let's create a scenario like the following image in which the IsTrigger of the red cube is ticked (since IsTrigger is an attribute on the trigger, both objects need to be added with collider. Let's not add rigidbody for the time being, and add a script to the red cube).

When running the game, we found that the red cube would pass through × × cube but did not print a word.

So the trigger condition is based on the collision, so we add rigidbody to the two cube, and cancel the gravity option.

The red cube passes through × × cube and calls three callback functions of the trigger, where

The stay function has been called 50 times, indicating that the red cube repeatedly calls the stay trigger during the period when it passes through × × × cube (overlap).

Trigger conclusion

1. Unlike collision, no matter who touches who triggers, the result is the same.

2. Necessary conditions for trigger: at least one Collider has IsTrigger checked, and at least one object has a rigid body before it will trigger.

3. The connections and differences between collisions and triggers and when to use them

Because the collision can trigger the event at the same time of the physical effect, and the trigger is the trigger event without physical effect.

Therefore, it is better to treat it on a case-by-case basis and apply it flexibly when you know the principle.

Let me give you an example.

There will be a collision when the football hits the wall, and when the football is out of bounds, there will not be a collision, but the referee's whistle will be triggered.

Then you can add a collision object to the wall without checking IsTrigger, but add a Collider and check IsTrigger.

Attention points related to collision and detection

Two points of attention

Note 1:

If two objects collide, if the collision information is to be detected, then one of the objects must have both a collider and a rigid body, and the script for detecting the collision information must be attached to the collider with a rigid body.

Note 2:

Two GameObject collisions, to detect the trigger message, at least one rigid body Collider and check the IsTrigger check box, the other must have at least one Collider component, at this time the collision detection script must be attached to the trigger with a rigid body.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report