In the Project panel, click the Small triangle next to Create, and selectJavaScript"Create a JS named" Collision "file。 Double-click it and the system starts monodevelop to edit the JS file.
Clears the code created by default inside. Write it up.
functionOncollisionenter (obj:collision)
{
DeBug. Log (obj.GameObject.name);
}
The Oncollisionenter function explains: This function is called when a bound object collides with another object.
obj parameters Explanation: The number of objects being hit, The type is collision. In this case, the "floor". Carrying variable Gameobject is the object we collide with name, which is the name we set for the floor.
< Span style= "padding:0px; margin:0px; Line-height:normal "> Drag the script file onto the ball to implement binding:
mouse left button is not placed. Drag directly into the Hierarchie panel, place it on the cube, and then complete the binding by releasing the mouse.
< BR style= "padding:0px; margin:0px; Line-height:normal "> dot top of the small triangle execution program:
program execution results, When the ball collides with the floor, the Unity3d bottom status bar will output the floor name
&NBSP;&NBSP;
< BR style= "padding:0px; margin:0px; Line-height:normal "> fix.
Oncontrollercolliderhit (), which is used for character collisions. The object assumes that the Charactor Controller (Unity's own script for role control) is used to detect collisions using this function, which is not required to add a rigid body. Collisions between normal rigid bodies use Oncollisionenter (), range triggering with ontriggerenter (), assuming you are letting two cubes collide. Make sure that one of the rigidbody is attached,
Collision is a variable. It enables you to obtain the properties of the target you are colliding with, as well as the collision point information and relative speed, such as collision.collider. A component or script. void Oncollisionenter (Collision collision) {if (collision.collider.tag== "enemy")//tag is the label of the object, which corresponds to the category name of the object.
On the top of each object's component bar (that is, the area where the script is added), the default is untagged, which is no tag, you can find it. Then click will appear a drop-down list, the default has a few tags, you can also add their own definition. Click the drop-down list at the bottom of the add tag to be able to add tag, and finally select a tag as the object name can be.
{Destroy (Collision.collider.gameObject);//Assuming that the opponent is detected as an enemy, destroy each other.
}}
Simple collision detection with Unity3d game development