Unity 3D-使用Collider2D处理GameObject的碰撞 (Unity 3D - Handling Collisions of GameObject using Collider2D)
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.
因此,让我们使用对撞机的这种知识。 是的,我们甚至不必使用脚本。 在您的项目中,打开您的主角,然后通过添加组件→物理2D→Box Collider来添加 Box Collider 2D 。 现在,您会注意到一个新组件Box Collider出现在“ 检查器”窗格中 。
Of course, the size value may vary depending on how your image is shaped.
当然, 大小值可能会根据图像的形状而有所不同。
You see the Edit Collider option that we have highlighted in the above image. If you click on that button, you'll notice that your image now has a few handles which you can use to adjust the size of the collider region.
您会看到我们在上图中突出显示的Edit Collider选项。 如果单击该按钮,您会注意到图像现在具有一些手柄 ,可用于调整对撞机区域的大小。
If you run the game right now, you won't really notice any difference. Your character will still move like normal using the arrow keys. So, let's test out the real magic of colliders by giving our main character something to collide with. To do so, right click in the Assets area and go to Create → Sprites.
如果您现在运行游戏,您将不会真正注意到任何区别。 使用箭头键,您的角色仍然会像往常一样移动。 因此,让我们为主要角色提供一些可以碰撞的东西,以测试碰撞器的真正魔力。 为此,右键单击Assets区域,然后转到Create→Sprites 。
This menu gives you a lot of basic shapes which you can use as placeholder sprites or for testing out interactions. Click on Square and it will generate a new Square shape for you to use.
此菜单为您提供了许多基本形状,可以将它们用作占位符精灵或用于测试交互作用。 单击正方形 ,它将生成一个新的正方形形状供您使用。
Press Enter, and then drag and drop the Square into the Hierarchy, just like we added Mr. Star.
按Enter,然后将Square拖放到Hierarchy中 ,就像我们添加Star先生一样。
Use the blue handles to resize the square to a decent size, so that you can test out the collisions. Something like the following image should suffice.
使用蓝色手柄将正方形调整为合适的大小,以便可以测试碰撞。 如下图所示就足够了。
If you run the game now, you'll see that Mr. Star doesn't really care for the obstacle in his way, and he'll simply slide smoothly under the white block.
如果您现在运行游戏,您会发现Star先生并不太在意自己的障碍,他会在白色方块下顺滑滑动。
How do we fix that? Here's how: Without changing anything else, add a Box Collider 2D to the new Square sprite as well. Since it's a basic rectangular shaped object, Unity will automatically add a Collider region exactly like the sprite shape. Now again, try and run the game.
我们该如何解决? 方法如下:在不进行任何其他更改的情况下,也将Box Collider 2D添加到新的Square精灵中。 由于它是基本的矩形对象,因此Unity会自动像精灵形状一样自动添加对撞机区域。 现在再次尝试运行游戏。
Our character now collides with the white block like we want it to! But as soon as you try to turn the character around the block's corner while pushing against it, things start going a little wonky.
我们的角色现在像我们希望的那样与白色方块碰撞! 但是,一旦您尝试在将角色推向该角色的角落时将其转过一个角落,事情就会开始变得有些奇怪。
Well, let's have a look at the whole scenario from Unity's point of view. While playing the game, simply click on the Scene tab to see what's going on behind the scene. If you click on your main character, you might see something like this:
好吧,让我们从Unity的角度来看整个场景。 在玩游戏时,只需单击“ 场景”选项卡即可查看场景背后的情况。 如果单击您的主角,您可能会看到类似以下的内容:
There's the problem! You tried to push the square collider against a corner, thus rotating it. In fact, here's a way you can test out this in real life, without even getting up from your computer seat. Try pushing the monitor (gently!) in the middle. It'll shift back a bit, right? But try pushing it against a corner. What happens? Your monitor rotates. If you've studied a bit of advanced physics, you'll know that what you're doing is applying a torque to your monitor, making it turn.
有问题! 您试图将方形对撞机推向某个角,从而使其旋转。 实际上,这是一种您可以在现实生活中进行测试的方法,而无需从计算机座位上站起来。 尝试(轻轻地)将显示器推向中间。 它会向后移一点,对吧? 但是,尝试将其推向某个角落。 怎么了? 显示器旋转。 如果您学习了一些高级物理学,就会知道您正在做的是向显示器施加扭矩,使其旋转。
Back to the game, how do we fix our character from going wonky around a corner? It's simple. Simply open up your main character, and in the Rigidbody2D properties, open up the Constraints section.
回到游戏中,我们如何解决角色从拐角处走弯路的问题? 这很简单。 只需打开您的主要角色,然后在Rigidbody2D属性中打开“ 约束”部分。
If you open that menu (by clicking the triangle), you'll see options to disable (or freeze, as Unity calls it) the object's movement and rotation. Just tick the Freeze Rotation Z box, and you're good to go. But it still isn't quite satisfactory. There's a big spacial gap between the two colliders where you can't move because of the box shape of the colliding regions.
如果打开该菜单(通过单击三角形),将会看到用于禁用(或冻结,如Unity所称的)对象移动和旋转的选项。 只需勾选“ 冻结旋转Z”框,就可以了。 但这还不是很令人满意。 由于碰撞区域的盒子形状,两个对撞机之间存在很大的空间差距,您无法移动。
How do we make the collider a bit more realistic in shape? Just like our star. Well, instead of a Box Collider, let's move on to the slightly more complex version of it, the Polygon Collider. To add a Polygon Collider 2D, let's first get rid of the box collider that's already attahced to our character. Simply click on the gears (to open settings) on the top right of the component (We have highlighted it in the picture), and select the option, Remove Component.
我们如何使对撞机的形状更逼真? 就像我们的明星。 好吧,让我们继续使用它的稍微复杂一点的Polygon Collider代替Box Polygon Collider 。 要添加Polygon Collider 2D ,首先让我们摆脱已经附加到角色上的盒子对撞机。 只需单击组件右上角的齿轮(以打开设置)(我们在图片中突出显示了它),然后选择选项删除组件 。
Now follow the same steps that we followed while adding a Box Collider, but this time, select a Polygon Collider instead. If your character has a fairly simple shape, Unity shouldn't have much problems in defining a similarly shaped collision region automatically for you. If it doesn't, however, as was the case for Mr. Star, we will have to do it on our own.
现在,按照添加Box Collider时遵循的相同步骤进行操作,但是这次,请选择Polygon Collider 。 如果您的角色的形状相当简单,那么Unity自动为您定义形状相似的碰撞区域应该不会有太大问题。 但是,如果不是这样,就像Star先生那样,我们将不得不自己做。
Simply click on the Edit Collider button to edit the collider's boundary region.
只需单击“ 编辑对撞机”按钮即可编辑对撞机的边界区域。
After a bit of editing on Mr. Star's collider, this is the final product we came up with:
在对Star先生的对撞机进行了一些编辑之后,这是我们想到的最终产品:
Try testing out your new collider in the game.
尝试在游戏中测试您的新对撞机。
Now, you may want to save your work. You can simply save it by pressing Ctrl + S, and saving it as a scene. We have explained how scenes work in previous lessons, and further down the road, we will also teach you how to work with multiple scenes.
现在,您可能要保存您的工作。 您只需按Ctrl + S并将其保存为场景即可 。 在上一课程中,我们已经说明了场景的工作原理,并且在以后的课程中,我们还将教您如何处理多个场景。
练习练习 (Practice Exercise)
-
Try making the player bound inside the game, so he doesn't wander off-screen. Put colliders to use by making new sprites.
尝试使玩家束缚在游戏内,以免他在屏幕外徘徊。 通过制作新的精灵来使用对撞机。
HINT: The main camera is standing still, so you have a fairly good idea of where to put the colliders by judging against the edges of the camera.
提示:主摄像头静止不动,因此您可以通过判断摄像头的边缘来确定对撞机的放置位置。
- Start and 开始”和“ Finish text in them and placing them in the game to help the player. 完成”文本的图像,并将其放置在游戏中以帮助玩家。
翻译自: https://www.studytonight.com/game-development-in-2D/colliders-and-collisions