Author: Wang Xuan Yi, source: http://www.cnblogs.com/neverdie/Reprinted, Please keep this statement. If you like this article, click [recommendation ]. Thank you!
Introduction
In the first article [basic Unity3D tutorial], we will show you the Unity tutorial for beginners (1): GameObject, Compoent, Time, Input, and Physics. I have already talked about the relationship between rigid bodies and collisions, this time, we will use the Flappy Bird case to explain the specific application of rigid bodies and collision bodies in the game. For the relevant code, see the source code of Flappy Bird.
Know RigidBody
Note that when Gravity Scale is set to 0, this rigid body is no longer affected by Gravity.
Understanding Collider2D
In the Add Component option, click Add Collider2D in Physics2D to Add a collision tool. The collision tool has the following three types:
- BoxCollider2D: box-shaped collision tool, generally a rectangle.
- CircleCollider2D: Circular collision tool.
- PolygonCollider2D: polygon collision
When we add a Collider for a 2D GameObject, the Collider will automatically select the most appropriate size, but you can use Shift + left mouse button to adjust the border of Collider. You can also use Ctrl + left mouse to delete a polygon vertex.
Of course, for the same rigid body, we can add multiple collision mentions for this rigid body to construct a more complex collision structure. You can see the corresponding examples in the official rock Demo-2D Platformer.
In addition, we can combine animation and rigid body motion to create more interesting motion effects. The transformation through Transform and RigidBody is not necessarily required, for more information about Animation, see my blog: [basic Unity3D tutorial] Unity tutorial for beginners (3): Learn about Sprite and Animation in Native 2D by making Flappy Bird.
About Is Trigger
If the Is Trigger option Is selected for a collision generator, it will not collide with a rigid body with another collision generator that does not check Is Trigger, but will "Trigger Collision", that Is, in this case, the Trigger message instead of the Collision message is sent during the Collision. In the script, we need to reload OnTriggerEnter instead of OnCollisionEnter.
Summarize Collision and Trigger. Which messages will be sent when you select certain attributes: