Collider and Trigger [Unity]

Source: Internet
Author: User

Please look at the original post, step: Unity3d Collision detection with the difference between the collider and the trigger

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.

The necessary condition for an object to collide: 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 the use of collider, the other is the use of triggers。 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 the trigger 3.monobehaviour.ontriggerstay (Collider Collider) when the stay trigger collision information is detected: 1.monobehaviour.oncollisionenter (collision Collision) When entering the collider 2.monobehaviour.oncollisionexit (collision collision) when exiting the collider 3.monobehaviour.oncollisionstay (  Collision collision) When the stay collider two objects in contact with the implementation of Ontriggerenter or the Oncollisionenter method, what is the difference between these two? 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 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 it is possible to use the trigger example: Collider: The car was crashed, the ball fell on the ground and the effect trigger: the door automatically opens when the person stands near the door. The approximate order of performance and efficiency of Collider is: Sphere Collider > Capsule > Box Collider > Mesh Collider.

A collider is another component that, together with a rigid body, takes a collision. If two rigid bodies collide with each other, the physics engine will not calculate collisions unless they contain a collider component. Rigid bodies without collider can penetrate each other in a physical simulation. The collider must be added to the object independently of the rigid body. The collider does not necessarily need to attach a rigid body, but to make a moving object have a collision effect, you must attach a rigid body. To make a collision event when two triggers collide, one of them must contain a rigid body.

Add a collider by Component->physics menu.

Box Collider Box Collider-the basic shape is a box.

Sphere Collider Ball Collider-The basic shape is a ball.

Capsule Collider Capsule Collider-The basic shape is a capsule.

Mesh Collider Mesh Collider-Creates a collider from the mesh of the object. Cannot collide with other mesh collider.

Wheel Collider Wheel Collider-a special collider for creating cars or other mobile vehicles.

Collider Properties:

Material Material

is Trigger whether the trigger

Radius radius

Center Centre

Size of size of the collider on the X, Y, and Z dimensions.

Mesh Mesh

Smooth Sphere Collisions Smooth character collision when activated, the collision mesh normals are smooth. It should be activated for smoothing surfaces such as the undulating surface composed of smooth, angular fluctuations.

If the convex is active, the mesh collider will collide with other mesh collider. A raised mesh collider is limited to 255 triangular polygons.

The radius and height of the capsule collider can be individually adjusted. A role controller that is placed between its poles, or used in combination with other collider for irregular shapes.

Using a Combo collider is a good choice when you have a complex grid that doesn't work with mesh collider. To create a combo collider, create sub-objects for your colliding objects, and then create a basic collider for each sub-object. This allows you to easily move, rotate, and shrink each collider independently.

Mesh collider usually does not collide with each other, but if a mesh collider is marked as raised (convex), it can collide with other mesh collider. A typical solution is to use a basic collider for moving objects and a mesh collider for static environment objects.

The collision mesh uses the back side culling (or the back selection, which means that the front polygon blocks the back polygon behind it, and the Direct3D will improve efficiency by picking (i.e. removing excess processing) back polygons. If an object collides with a mesh that is removed from the back, it will not collide physically.

Static Collider colliders

A static collider is a game object that contains a collider but does not contain a rigid body. A static collider is used to always stay in the same place without moving horizontal geometry. You can add a grid collider to an existing graphics grid (even better than using the input settings to generate a collider check box), or you can use another type of collider.

Instead of moving a static collider one frame at a time, moving the static collider will cause the internal reset of the PhysX engine, which is very resource intensive and can cause a significant decrease in performance. In addition, the behavior of waking other rigid bodies based on a static collider is undefined, and moving the static collider will not exert friction on the rigid body that touches it. Instead, the collider to be moved remains a kinematic rigid body.

Layer-based Collision Detection Layer-based collision detection

After the Unity3 release, we introduced something called "Layer-based Collision detection" (layer-based collision Detection) that allows you to selectively control the unity objects and their attached specific layer collisions.

Select the layer of the selected game object.

Edit->project Settings->physics., open the physical reference interface. Select the layer that you want to interact with the other layers in the collision matrix, and tick.

Also, look at the physical crash experiment that was done: "Unity" Physics Crash experiment

The article mentions the Unity component to divide the collision body into 3 types:

Static Collider Collider

These is gameobjects that does not has a rigidbody attached, but does have a Collider attached. These objects should remain still, or move very little. These work great for your environment geometry. They won't move if a rigidbody collides with them.

Refers to a game object with a collider attached without a rigid body attached. This type of object will remain stationary or move very slightly. It works well for environmental models and does not move when colliding with rigid bodies.

Rigidbody Collider Rigid Body Collider

These gameobjects contain both a rigidbody and a Collider. They is completely affected by the physics engine through scripted forces and collisions. They might collide with a gameobject that is only contains a Collider. These'll likely be your primary type of Collider on games so use physics.

Refers to a game object with both a rigid body and a collider attached. The power and collisions through the script are completely affected by the physics engine. You can collide with a game object that contains only the collider. Will be the basic type of collider that uses physical effects in your game.

Kinematic rigidbody Collider kinematic Rigid Body Collider

This gameobject contains a Collider and a rigidbody which are marked iskinematic. To move the Gameobject, you modify its Transform Component, rather than applying forces. They ' re similar to Static colliders but would work better when you want to move the Collider around frequently. There is some other specialized scenarios for using the This gameobject.

Refers to a type of game object that contains both a collider and a rigid body, and activates iskinematic, to move such a game object, to modify its transform component (referring to attributes such as position and rotation), rather than force. They're much like Static collider, but they'll work better if you want to keep moving the collider around. There are many other unique usage scenarios for this type of game object.

Kinematic Rigid Body Collider is a very lazy and overbearing guy, it encounters other collider completely unresponsive (in fact, a little reaction, will trigger some collision function, followed by explanation). It is unaffected by force, gravity, or torque. You can manipulate them accurately or move them by setting transform position and rotation, but they can interact with other non-kinematic rigid bodies. For a rigid body placed above a kinematic rigid body collider, the friction force exerted by the kinematic rigid body is affected.

There is also a class called role controller.

Character Controllers Role Controller

You use the Character Controllers if you want to make a humanoid Character. This could is the main character in a third person platformer, FPS shooter or any enemy characters.

If you want to create a character that resembles a person, use a role controller. This can be the third person platform game, the main character of the first person shooter or any hostile character.

These Controllers don ' t follow the rules of physics since it'll not feel right (in Doom you run all miles per hour, come To halt in one frame and turn on a dime). Instead, a Character Controller performs collision detection to make sure your characters can slide along walls, walk up a nd down stairs, etc.

Such controllers do not follow the physical rules so it feels wrong (in Doom, you run 90 miles per hour, then stop immediately and you can turn around very quickly). However, the role controller performs collision detection to ensure that your character can slide along the wall, up and down the stairs, and so on.

Character Controllers is not affected by forces and they can push rigidbodies by applying forces to them from a script. Usually, all humanoid characters is implemented using Character Controllers.

The role controller is not affected by force (but the gravitational effect is still there) but can be driven by the force exerted by the code. Typically, all similar roles are performed with a role controller.

Character Controllers is inherently unphysical, thus if you want to apply real physics-swing on ropes, get pushed by bi G Rocks-to your character you have the use of a rigidbody, this would let you use the joints and forces on your character. Character Controllers is always aligned along the Y axis, so you also need to use a rigidbody if your Character needs to Be able to change orientation in space (for example under a changing gravity). However, be aware this tuning a rigidbody to feel right for a character are hard due to the unphysical it in which game Ch Aracters is expected to behave. Another difference is this Character Controllers can slide smoothly over steps of a specified height, while rigidbodies wi ll not.

The character controller itself is not physical, so if you want to apply a real physical effect-swinging on a rope, pushed by a big rock-to your character, you have to use a rigid body, which allows you to use hinges or forces on your character. The role controller is always aligned along the Y axis, so if your character needs to change direction in space it must also use a rigid body (for example, under varying gravitational control). However, it is very difficult to realize that adjusting a rigid body in character is natural, because the non-physical characteristics of the game character are always wanted to show. Another difficulty is that the character controller can slide down smoothly from a certain height of stairs, while the rigid body does not.

The Controller does not react to forces in its own and it does not automatically push rigidbodies away.

The controller does not react to forces that are added to itself, nor does it automatically push other rigid bodies away. (I don't understand it here because I experimented with instantiating a controller inside a rigid body that would bounce off.) If anyone understands, please let me know and appreciate it! )

If you want to push rigidbodies or objects with the Character Controller, you can apply forces to any object that it Colli Des with Via the oncontrollercolliderhit () function through scripting.

If you want the role controller to push aside other rigid bodies or objects, you can add the Oncontrollercolliderhit () function to the object's attached script so that exerting force on them can result in collisions.

That is, the role controller can use physical effects to influence other objects, provided that you write the script yourself.

On the other hand, if you want your player character to being affected by physics then you might be better off using a rigidb Ody instead of the Character Controller.

The role controller cannot be affected by other objects by physical effects. So if you want your game character to be affected by physical effects, it's best to use a rigid body instead of a character controller.

Character controllers and kinematic rigid bodies are a bit like, because they are not forces (kinematic rigid bodies more like a rigid body, but not affected by any other forces, like the ancient kings, although the same as ordinary civilians are human, but he can only bully others, others can not bully him , and the role controller itself is not physical, can not push others (unless you write your own script), will not be affected by others, but it can perform collision detection). But there are a couple of different places. First, a kinematic rigid body cannot be driven by the force exerted by the Code, but the role controller is possible. Second, the kinematic rigid body is not affected by gravity, and the role controller has a gravitational effect. Second, the role controller can only rotate along the y axis, that is, people are always upright, not because of the effect of force on the ground, and the rigid body is OK. Third, the role controller can slide down smoothly from a specific height step, while the rigid body does not.

Said so much, dizzy, well, I also faint. So let's do the experiment! We experiment with one of the simplest scenarios, a parallel light, a plane, and two cubes. See if a collision occurs by adding different collision types to two cubes.

In the following experiment, one cube remains stationary, and the other cube uses code to control motion, that is, the upper and lower left and right arrow keys can control the movement of the upper and lower sides.

The Moving static collider vs stationary Static collider has no collision effect.  This is expected, because no one has added the Rigdbody attribute, certainly will not collide! The Moving Static collider vs stationary Rigid body Collider has no collision effect. This result is a bit unexpected, obviously one has already added rigdbody, why still can't happen collision? This is because the cube with the rigdbody added is stationary, while the still-just experience goes into hibernation. Well, again, a new noun, a rigid body sleeps. For example, when a cube falls on the floor and is stationary, it enters a rigid body to sleep. Rigid body hibernation occurs completely automatically. As long as the rigid body is slower than sleepangularvelocity and sleepvelocity, the rigid body begins to hibernate. After it has been idle for some frames, it is set to hibernate. Objects that are in hibernation are no longer subject to collision detection and simulation. This can save a lot of CPU overhead. That explains why we didn't have a collision in this experiment because it went to sleep, and the moving Static collider couldn't wake it up, so it didn't even know that someone had met. Here's another question, so under what circumstances can you wake up a dormant rigid body? There are 4 in the situation:
    • An external force is applied, which means that the addforce is used in the code;
    • The properties of the rigid body have changed;
    • And it moves through the joint of the rigid body, because of the associated relationship, so it is also awakened;
    • collided with other rigid body collider. However, only rigid bodies in motion (including ordinary rigid-body collider and kinematic rigid-body collider) can awaken a rigid body in hibernation, and a static collider cannot wake a rigid body in hibernation. That is, if you bind a static collider (which is not bound to any rigid body) to a rigid body in hibernation, or when you pull a static collider away from a rigid body in hibernation, this dormant rigid body is not awakened. But if you move a moving rigid body from the top-level rigid body that it belongs to and is dormant, the rigid body in hibernation will be awakened and will be recalculated correctly in the graphical update.
What do you mean by this last sentence? Let's do two more experiments. Or two cubes, we put one cube above the other.   In two experiments, the upper cube is a normal rigid-body collider, and the Lower cube is different, and we can move the cube underneath the code to move it from below the upper cube. In the first experiment, the Lower cube is a static collider. When we move, the upper cube drops straight down until it touches the floor without any physical effects (except the gravitational effect). In the second experiment, the Lower cube is a kinematic rigid-body collider. As we move the cube below, we will find that the upper square is affected by the friction and the movement has also taken place, and when the two cubes are completely detached, the upper cube is shaking and falling onto the floor (because it was previously subjected to friction). All right, you got it? A static collider does not wake up a dormant rigid body (the first experiment), and a kinematic rigid body can wake a rigid body in hibernation because it forces the body to force it.   One more question, what if the cube above is a kinematic rigid-body collider? So no matter what the collider is, it's not responding, because it's the king, not affected by any external forces, including gravity!    Moving Rigid body collider vs stationary Static collider has a collision effect, specifically: for moving rigid-body collider after encountering a static collider will produce physical effects, such as rebound, unable to advance, etc. for stationary Static collider, there will be a slight collision effect, such as a slight shaking, But it won't move.    Moving Rigid body collider vs stationary Rigid body Collider has a collision effect, with two objects having normal physical effects, such as bounce.    moving Static collider vs stationary kinematic Rigid body Collider has no collision effect. The Kinematic rigid body Collider is not subjected to any external force.    Moving kinematic Rigid body collider vs stationary Static collider has no collision effect. The Kinematic rigid body Collider is not subjected to any external force.    Moving kinematic Rigid body Collider vs stationary Rigid body Collider has collision effect, but only rigid body collider has collision effect, such as rebound, kinematic rigid body Collider is not force.    Moving Rigid body collider vs stationary kinematic Rigid body Collider has collision effect, but only rigid body collider has collision effect, such as rebound, kinematic rigid body Collider is as motionless as a wall.    Moving kinematic Rigid body collider vs stationary kinematic rigid-Body Collider two Overlord what happens when we meet each other? No response ... Just like two static collider.     below, replace a cube with a character controller to continue the experiment.  &nbsThe p;  moving Static collider vs Stationary role Controller does not have any impact effect.    Moving Rigid body collider vs stationary role controller has a collision effect, the rigid body collider will bounce back after encountering the role controller, unable to move forward, and the role controller has no effect.    Moving kinematic Rigid body Collider vs stationary character Controller has no collision effect and will cross each other.    Mobile role controllers vs stationary Static collider have no collision effect and will cross each other.     Mobile Role Controller vs stationary Rigid body Collider has a collision effect, and a rigid body collider is bounced off.    Mobile Role Controllers vs stationary kinematic Rigid body collider have no collision effect and will cross each other.      collision information and trigger information   whether a collision function (or trigger information) is emitted during a collision, see the table below. Collision information refers to the three functions of Oncollisionenter () Oncollisionstay () and Oncollisionexit (), and the triggering information refers to the Ontriggerenter () Ontriggerstay and Ontriggerexit () three functions.   
Collision detection occurs and messages are sent upon collision
Collision detection after collision with collision information issued
Static Collider
Static Collider
Rigidbody Collider
Rigid body Colliding device
Kinematic
Rigidbody Collider
Kinematic Rigid Body Collider
Static
Trigger Collider
Static Trigger Collider
Rigidbody
Trigger Collider
Rigid Body Trigger Collider
Kinematic Rigidbody
Trigger Collider
Kinematic Rigid Body Trigger Collider
Static Collider Collider Y
Rigidbody Collider Rigid Body Collider Y Y Y
Kinematic Rigidbody Collider
Kinematic Rigid Body Collider
Y
Static Trigger Collider
Static Trigger Collider
Rigidbody Trigger Collider
Rigid Body Trigger Collider
Kinematic rigidbody Trigger Collider
Kinematic Rigid Body Trigger Collider
Trigger messages is sent upon collision
Trigger message after a collision
Static Collider
Static Collider
Rigidbody Collider
Rigid body Colliding device
Kinematic
Rigidbody Collider
Kinematic Rigid Body Collider
Static
Trigger Collider
Static Trigger Collider
Rigidbody
Trigger Collider
Rigid Body Trigger Collider
Kinematic Rigidbody
Trigger Collider
Kinematic Rigid Body Trigger Collider
Static Collider Collider Y Y
Rigidbody Collider Rigid Body Collider Y Y Y
Kinematic Rigidbody Collider
Kinematic Rigid Body Collider
Y Y Y
Static Trigger Collider crash trigger Y Y Y Y
Rigidbody Trigger Collider
Rigid Body Trigger Collider
Y Y Y Y Y Y
Kinematic rigidbody Trigger Collider
Kinematic Rigid Body Trigger Collider
Y Y Y Y Y Y

Collider and Trigger [Unity]

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.