Unity 3D-使用Collider2D处理GameObject的碰撞

本文介绍了Unity 3D中如何使用Collider2D组件处理游戏对象之间的碰撞。Collider2D定义了游戏对象的碰撞区域,允许开发者通过调整碰撞边界来实现更精确的碰撞检测。通过添加Collider2D到游戏对象并编辑其形状,可以创建更符合实际物理行为的碰撞效果。此外,文章还提到了解决角色在角落碰撞时旋转的问题,通过冻结Rigidbody2D的旋转以及使用Polygon Collider 2D来创建更复杂的碰撞形状,使游戏物理行为更加真实。
摘要由CSDN通过智能技术生成

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,大多数开发人员都使用基本形状来制作近似形状。

Handling Collisions of GameObject using Colliders

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.

因此,让我们使用对撞机的这种知

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值