"Getting Started with unity" collision detection and triggering detection

Source: Internet
Author: User

Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

In unity, the collision of a game object can be detected by a rigid body assembly (rigidbody) and a collider component (Collider). First, add a plane panel in the scene as the ground, and then place a cube cube at a height above the plane panel.

Then add a rigid body component (Rigidbody) to the Cube cube. When we run the game, we can see that the cube falls on top of the panel.

The cube and panel have a collision and are forbidden on the panel, and we can use the script to detect it, such as when the Cube falls on the panel and we can destroy it. Add a script component to the cube to detect collisions Cubecollision, language uses C #. The code is as follows:

1 /**2 * Copyright (c) Clarence Zeng Binsi3  * 4 * Author:zengbinsi5 * DATE:2016/01/25 16:10:286 * desc:collision Test7  */8 9 Ten  One  A usingUnityengine; - usingSystem.Collections; -  the  Public classCubecollision:monobehaviour { -  -  -     //Use this for initialization +     voidStart () { -  +     } A  at     //Update is called once per frame -     voidUpdate () { -  -     } -  -  in  -  to     //Collision Start +     voidOncollisionenter (Collision collision) { -         //destroying the current game object theDestroy ( This. Gameobject); *     } $ Panax Notoginseng     //End of collision -     voidOncollisionexit (Collision collision) { the  +     } A  the     //the impact continues +     voidOncollisionstay (Collision collision) { -  $     } $  -}

In the Monobehaviour class, Oncollisionenter, Oncollisionexit, and Oncollisionstay are callback methods at collision time, and we can overload them in the Cubecollision class. When a game object that is bound to the Cubecollision Script component collides, the oncollisionenter is triggered to be called once. The Oncollisionstay method is then continuously invoked throughout the collision process until the collision contact is lifted and the oncollisionexit is triggered once. This is a complete collision contact process, and all three methods have a collision type parameter that holds the collision information.

If we want to manipulate the object (panel) that the cube collides with in the current script, we can collision the Collider Component Object (collider) of the colliding game object by the parameters of these three methods. The collider object can then be used to get information about the game object mounted by the collider component.

1     // Collision Start 2     void Oncollisionenter (Collision collision) {3         var name = collision.collider.name; 4 5         Debug.Log ("" + name); 6     }

In this way, you can get the name of the game object that is colliding.

If we have a lot of game objects, the names are different, if we want to detect a certain type, we can differentiate by setting the tag value for the game object. We set a tag value for the Panel object:

Then modify the code to get the tag value:

1     // Collision Start 2     void Oncollisionenter (Collision collision) {3         var tag = Collision.collider.tag; 4 5         Debug.Log ("" + tag); 6     }

Re-run the game:

In addition to collisions, unity also supports contact detection. Because of collision detection, there is a collision simulation between the game objects that collide, such as hitting something and bouncing or pausing. Sometimes we just want to check if there is contact between the object and the object, but do not have the effect of collision, we can use the trigger to detect the contact.

First, we set the collider of Cube cube to trigger type:

In this case, we found the collider component in the check view of cube and checked the Istrigger property of the component so that there would be no impact when the game object touched, but would go straight through. Unity Collider has many types, cube collider type is Box Collider (Boxcollider), in addition to spherical collider, capsule collider and so on.

We then reload the following three methods in the Cubecollision Script component:

1     //Start Contact2     voidOntriggerenter (Collider Collider) {3Debug.Log ("Start Contact");4     }5 6     //End of contact7     voidontriggerexit (Collider Collider) {8Debug.Log ("End of contact");9     }Ten  One     //Contact continued in A     voidOntriggerstay (Collider Collider) { -Debug.Log ("Contact continued in"); -}

The Ontriggerenter, Ontriggerexit, and Ontriggerstay of the Monobehaviour class are the three callback methods that trigger detection, which is called once when the game object is in contact. Ontriggerexit is called once when the game object is completely detached, while the ontriggerstay is continuously called during the game object's contact.

  "It is worth noting that the parameters of these three methods of the trigger callback are the collider type, which represents the trigger component object of the game object being collided. 】

Rerun the game and we can see the debug information in the console:

This time the cube will go straight through the plane and will not stop on the panel.

"Getting Started with unity" collision detection and triggering 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.