[Unity3D] Collision Detection for Unity3D Game Development

Source: Internet
Author: User

Hello, everyone. Welcome to the Unity3D Game Development Series articles that I bring to you. My blog address is http://blog.csdn.net/qinyuanpei.

Today, let's learn a very important concept in Unity3D: collision. Why is collision important? Because the game is full of collisions all the time, in flight games, we need to determine whether the shells hit the enemy. In RPG games, we need to determine whether the players have caused damage to the enemy, in a shooting game, we need to determine whether the bullet has hit the enemy ....... all in all, the so-called sense of attack in the game is the collision we are going to talk about today. In terms of concept, an object is affected by the force of another object and changes its position or State. This is called a collision. In Unity3D, the system has provided collision detection methods. Here we introduce two collision detection methods:


1. Collision Detection: using Collision detection requires the following three important methods: OnCollisionEnter () call this method immediately when the rigid body is in contact with the rigid body, OnCollisionStay () this method is called in the collision between a rigid body and a rigid body, And OnCollisionExit () is called at the end of the collision between the rigid body and the rigid body.


2. Trigger Detection: to use Trigger detection, check the IsTrigger attribute and use the following three important methods: OnTriggerEnter (): Start collision, OnTriggerStay (): collision, OnTriggerExit (): end the collision.


So what are the differences between the two detection methods? Collision will cause a physical Collision and Trigger will cause a non-physical Collision. At the same time, please bear in mind the following collision principles:

A. Trigger: both parties have a collision device, at least one side has a rigid body, and at least one side has checked the IsTrigger attribute.

B. The premise of Collision is that both parties have a Collision tool and at least one party has a rigid body.


Next we will look at a demo project, the first scenario to create.


Here we have made a judgment on the collision target. If we collide with the Sphere of the object, the method in the script of the target object will be executed and the collision information will be output. The script of the target object Sphere is similar to that of Cube, so it is not provided here. Finally, we can see the result. As we expected, the collision information is printed on the screen, and the console also outputs the corresponding information.

In this demonstration project, we will use the keyboard to control the left Cube to collide with the right sphere, then print the Collision Information on the screen, and output the script call information on the console.

First, let's look at some of the script code written for the Cube:

// One console output method public void Log (string msg) {Debug. log (msg);} // The Collision starts void OnCollisionEnter (collision) {if (Collision. gameObject. name = "Sphere") {Message = "Enter collision, collision name:" + collision. gameObject. name; collision. gameObject. getComponent <TargetScript> (). log (Message) ;}}// void OnCollisionStay (Collision collision) {if (collision. gameObject. name = "Sphere") {Message = "collision, collision name:" + collision. gameObject. name; collision. gameObject. getComponent <TargetScript> (). log (Message) ;}}// the void OnCollisionExit (Collision collision) {if (collision. gameObject. name = "Sphere") {Message = "collision ended, collision name:" + collision. gameObject. name; collision. gameObject. getComponent <TargetScript> (). log (Message); collision. gameObject. rigidbody. sleep ();}}


Why do we need to write two methods to output data to the console? In actual game development, when a collision occurs, we may need to do more practical things, such as reducing blood for the enemy and adding blood to us, here is just a simple understanding of the Unity3D collision mechanism. Trigger detection is similar to this, so I will not repeat it here. Thank you! Today's content is like this!

Related Article

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.