Collisions are Unity's way of understanding the physical interaction between two Rigidbodies, and indeed, two gameObjects in general. In real life, when we say two objects collided, we mean that they touched each other physically.
碰撞是Unity理解两个刚体以及实际上两个游戏对象之间的物理相互作用的方式。 在现实生活中,当我们说两个物体碰撞时,是指它们彼此物理接触。
In Unity, collisions aren't defined by Rigidbodies themselves, but instead by a new component called Collider2D. Collider2Ds are components which define a region in which collision interaction between gameObjects occur. In simple terms, colliders are simply the defined regions where gameObjects are solid to other gameObjects. We stress on the fact that they're regions because not all colliders are shaped exactly like the gameObject that defines them. In fact, for highly detailed models and sprites, most developers use basic shapes that make an approximate shape.
在Unity中,碰撞不是由刚体自身定义的,而是由一个名为Collider2D的新组件定义的。 Collider2D是定义游戏对象之间发生碰撞交互的区域的组件。 简单来说,对撞机只是游戏对象相对于其他游戏对象牢固的定义区域。 我们强调它们是区域的事实,因为并非所有对撞机的形状都完全像定义它们的gameObject一样。 实际上,对于高度详细的模型和Sprite,大多数开发人员都使用基本形状来制作近似形状。
In the above images, note the green boundaries. Those boundaries are what define the collision region in the respective gameObjects. In the triangle, the boundary fits the shape of the gameObject perfectly since we used a simple triangle. In the square, we made the collision region larger than the actual shape of the gameObject to once again make the point, that the Collider is an independent region from the gameObject.
在上图中,请注意绿色边界。 这些边界定义了各个游戏对象中的碰撞区域。 在三角形中,由于我们使用了一个简单的三角形,因此边界完全适合gameObject的形状。 在正方形中,我们使碰撞区域大于游戏对象的实际形状,以再次指出,对撞机是与游戏对象无关的区域。
The most interesting case is the Gear object. Note how the collider doesn't wrap around the gear's teeth perfectly, but instead Unity made approximations on the collision regions of the shape, leading to a simpler collision region. (We didn't make that collision region, Unity auto-generated it. We will show you how to do that yourself very soon).
最有趣的情况是Gear对象。 请注意,对撞机如何无法完美地缠绕齿轮的齿,而是Unity对形状的碰撞区域进行了近似计算,从而使碰撞区域更简单。 (我们没有创建该碰撞区域,Unity自动生成了该碰撞区域。我们将向您展示如何尽快实现该目标)。
So, let's put this knowledge of colliders to use. We don't even have to use scripting, yay! In your project, open up your main character, and add a Box Collider 2D
by going to Add Component → Physics 2D → Box Collider. You'll now notice that a new component, the Box Collider, has showed up in the Inspector pane.
因此,让我们使用对撞机的这种知