The difference between collider and trigger in Unity collision detection

Source: Internet
Author: User

To create a collision you must add a rigid body (rigidbody) and a collider to the game object, which allows the body to move under physical influence. A collision body is a class of physical components that are added to a game object with a rigid body to trigger collisions. If two rigid bodies collide with each other, the physical engine calculates collisions unless two objects have collisions, and in a physical simulation, the first experience without the collider crosses each other.

Necessary conditions for the collision of an object two objects must have a collider (Collider), and one object must also have a rigidbody rigid body. In Unity3d, there are two ways to detect collisions, one is to use a collider, and the other is to use a trigger. Collision Device:A group of components, it contains many kinds, such as: Box Collider (Box collision body), mesh Collider (Mesh collision body), and so on, these collider applications are different, but must be added to Gameobjecet body. Trigger, simply tick the Istrigger attribute selection box in the collider component in the Inspector. Trigger Information detection: 1. Monobehaviour.ontriggerenter (Collider Collider) when entering trigger 2. Monobehaviour.ontriggerexit (Collider Collider) when exiting Trigger 3. Monobehaviour.ontriggerstay (Collider Collider) when the stay Trigger collision information detects: 1. Monobehaviour.oncollisionenter (Collision collision) when entering Collider 2. Monobehaviour.oncollisionexit (Collision collision) when exiting Collider 3. Monobehaviour.oncollisionstay (Collision collision) when the stay collider two objects in contact with the Ontriggerenter or execute Oncollisionenter method, What difference does the two have? To verify the difference between the collider and the trigger, a simple game is designed to make the car collide with the ball. Create a new C # script file, drag it to the car object, double-click the script file into the Script Editor, add the following two methods to the script, and set a breakpoint separately:
void Ontriggerenter (Collider Collider) {     //Enter code executed by trigger}void Oncollisionenter (Collision collision) {     // Enter the code that the collider executes}
When the IS trigger option of the collider is not checked, the car collides with the sphere, enters the method Oncollisionenter, the car produces the effect of being hit by the sphere after the collision: If the is trigger property of the collider is selected, start the game again to make the sphere contact the car, Then enter the method Ontriggerenter. The sphere then passes directly through the car, but neither the sphere nor the car has a collision effect: what is the difference between a collider and a trigger in a unity3d? The collider is the carrier of the trigger, and the trigger is just a property of the collider when the is Trigger=false, the collider triggers a collision based on the physical engine, which produces a collision effect, and can call the Oncollisionenter/stay/exit function; when is Trigger =true, the collider is ignored by the physics engine and can call the Ontriggerenter/stay/exit function without a collision effect. If it is necessary to detect the contact of the object and do not want the collision detection to affect the movement of the object or to detect whether an object is passing through an area of space, then you can use the trigger example: Collider: The car was crashed, the ball fell on the ground and the effect trigger: people standing close to the door of the door automatically open effect another articleUnity3d understanding of Rigid bodies (Rigidbody) and Collider (Collider) and triggers (is Trigger), Edge learning Edge Update

The official form of the rigid Body (rigidbody) (from Unity3d's official guidebook "unity4.x to Mastery") is explained below:

The Rigidbody (rigid body) component allows the game object to be controlled in the physical system, and the rigid body accepts external forces and torque forces to ensure that the game object moves as it does in the real world. Any game object can be affected by gravity only by adding a rigid body component, and the action that is added by the script for the game object and the interaction between the Nvidia physics engine and other game objects requires the game object to add a rigidbody component.

Personal understanding is that a rigid body is used to simulate the performance of an object when it is subjected to a force, as can be seen from the parameters of a rigid body:

It is all about the parameters of how the object is subjected to force, such as mass mass, drag resistance.

The official of the collider (excerpted from Unity3d's official guidebook "unity4.x from Beginner to Mastery") explains the following:

A collision body is a class of physical components that are added to a game object with a rigid body to trigger collisions. If two rigid bodies collide with each other, the physical engine calculates collisions unless two objects have collisions, and in a physical simulation, the first experience without the collider crosses each other.

The individual is so understanding, why to judge the collision is the need to calculate the force, whether it is resistance or power, if the object has a rigid body assembly, then the object will be in force under the action of motion. If the object does not have a rigid body, then the force generated by the collision has no meaning, and the calculation of the collision has no meaning.

So, we can infer that if two colliding bodies have no rigid components, then the two objects will not collide if they collide with each other.

The collision on the official website is as follows (note: It is said to be the official website of the map, I have seen many places on the internet, but I have not found in the official website where ha):

From the above figure we can see that there must be a rigidbody on both sides of the collision, and both sides of the collision have to have colliding body components.

But when doing the experiment found a very strange thing, is that the subject of the collision must be with a rigid body, that is, only the collision body with a rigid body to hit other colliding bodies, the two sides will receive a collision event, the following is my experiment:

One, the control of a (rigid body and collision body) hit a stationary B (only collision body), the two sides can receive a collision event.

Second, control B (only collision body) hit stationary A (rigid body plus collision), the two sides do not receive the collision event.

Third, let a (rigid body and collision body) fall freely, and then control B (only collision body) to hit a, both sides can be affected by the collision event.

The conclusion seems to be that if there is only one rigid body on both sides of the collision, then the rigid body must be in a state of motion before a collision event occurs.

Next is the Istrigger option, which does not trigger the message, and first enumerates the triggering events and collision events, as follows:

Trigger Information Detection:
1.monobehaviour.ontriggerenter (Collider Other) when entering the trigger
2.monobehaviour.ontriggerexit (Collider Other) when exiting a trigger
3.monobehaviour.ontriggerstay (Collider Other) when stay trigger

Collision Information Detection:
1.monobehaviour.oncollisionenter (Collision Collisioninfo) when entering the collider
2.monobehaviour.oncollisionexit (Collision Collisioninfo) when exiting the collider
3.monobehaviour.oncollisionstay (Collision Collisioninfo) as a stay collider


Here is a diagram of the trigger, as follows (note: Also from the internet, I have not yet found on the official website ha)

Here is what I do test, is a object to hit B object.

A, a (collision body), B (no collider, no rigid body), no triggering event.

Second, A (collision body), B (collision body), no triggering event.

Two, A (collision body and rigid body, open Istrigger), B (collision body, close istrigger), both sides can receive the trigger event.

Three, A (collision body and rigid body, close istrigger), B (collision body, open istrigger), both sides can receive the trigger event.

Four, A (collision body, close Istrigger), B (collision body and rigid body, open istrigger), no triggering event.

V, A (collision body, open Istrigger), B (collision body and rigid body, close istrigger), no triggering event.

Additional experiments:

Six, a (collision body and rigid body, open istrigger) Free fall, B (collision body) hit A, both sides can receive trigger events.

According to the above experiment, if you want to receive a trigger event, the following three conditions must be met:

1, must have a collider component (Collider), in fact, the above collision events also need this prerequisite.

2, there must be an object with a rigid body assembly, and in the movement of the body (including the active movement to impact on others and in the course of the movement by others hit).

At least one of the 3, two collider Istrigger is turned on.

In addition, there are two points:

The two objects that generate the triggering event will cross each other, precisely because the object that is opened Istrigger is ignored by the physical engine lock, so two objects can be traversed.

Two, two objects either receive a collision event, or both receive a trigger event, no event is received, and a collision event is received.

Another point of experience is that when you control an object A to hit another object B, do not directly modify the position property of the transform of object A, because you will find that if even the two collisions are still going through, the reason is that we are directly set the coordinates, If object A has a rigid body component, it should use Addforce to add a force to the object, and then let the physics engine move the object so that if a collision occurs, it will not cross.

Well, in the afternoon after a long time, I have been thinking of that istrigger have what use, later on the internet search, said is can do the door automatically open, let me think of the Unity3d 4 of which angrybots example, seemingly when the control protagonist walked to a door, the door will automatically open.

So open the example project, to see, and found that sure enough, the principle is to put a set of istrigger in front of the transparent collision body, for special monitoring of the collision, when the protagonist into the area, the Ontriggerenter event to open the door beforehand.

The difference between collider and trigger in Unity collision detection

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.