To generate a collision, you must add the Rigidbody and the collision generator to the game object. The rigid body allows the object to motion under physical influence. A collision body is a type of physical component. It must be added to a game object together with a rigid body to trigger a collision. If two rigid bodies collide with each other, the physical engine will calculate the collision only when the two objects collide with each other. In physical simulation, the collision body does not pass through each other. A required condition for an object to collide. Both objects must carry a collider, and one object must also carry a Rigidbody rigid body. In unity3d, there are two ways to detect collision: one is to use the collision tool and the other is to use the trigger.
Collision tool:A group contains many types, such as box collider and mesh collider, which are used in different scenarios, but they must all be added to gameobjecet.
TriggerYou only need to check the istrigger attribute selection box in the interceptor component in the view panel. Trigger information detection: 1. monobehaviour. ontriggerenter (collider Collider) when entering trigger 2. monobehaviour. ontriggerexit (collider Collider) when the trigger exits 3. monobehaviour. ontriggerstay (collider Collider): 1. monobehaviour. oncollisionenter (collision) when entering the collision Server 2. monobehaviour. oncollisionexit (collision) When exiting the collision Server 3. monobehaviour. oncollisionstay (collision) Whether to execute ontriggerenter or oncollisionen when two collision objects are in contact What are the differences between the two methods? In order to verify the difference between the collision generator 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 to enter the Script Editor, add the following two methods to the script, and set a breakpoint respectively:
Void ontriggerenter (collider Collider) {// code for entering the trigger execution} void oncollisionenter (collision) {// code for entering the collision server execution}
If the is trigger option is not checked, the car will collide with the sphere and enter oncollisionenter. after the collision, the car will be hit by the sphere: if you select the is Trigger attribute of the collision generator, start the game again to bring the ball into contact with the car, and enter the ontriggerenter method. After that, the Sphere passes through the car directly, but the sphere and the car do not have a collision effect: What is the difference between the collision generator and the trigger in unity3d? The collision generator is the trigger carrier, and the trigger is only a property of the collision generator. When is Trigger = false, the collision generator triggers a collision based on the physical engine, resulting in a collision effect, you can call the oncollisionenter/stay/exit function. When is Trigger = true, the collision generator is ignored by the physical engine and has no collision effect. You can call the ontriggerenter/stay/exit function. If you want to detect the contact of an object and do not want the collision detection to affect the movement of the object, or check whether an object has passed through a certain area of space, you can use the trigger example: collision: A trigger is triggered when a car is hit, a ball is dropped to the ground, and a result is automatically opened when a person is standing near the door.