unity3d刚体_了解Unity 3D刚体

unity3d刚体

了解Unity 3D刚体 (Understanding Unity 3D Rigidbodies)

A rigidbody is a property, which, when added to any object, allows it to interact with a lot of fundamental physics behaviour, like forces and acceleration. You use rigidbodies on anything that you want to have mass in your game.

刚体是一种属性,将其添加到任何对象后,便可以使其与许多基本物理行为(例如力和加速度)相互作用。 您要在游戏中要增加质量的任何物体上使用刚体。

In other words, adding a Rigidbody to a gameObject is the way of telling Unity,

换句话说,向游戏对象添加刚体是告诉Unity的方法,

Okay, see this object here? I want it to behave like it has some mass and weight, and that it should react to forces and collisions in a realistic manner.
好吧,在这里看到这个物体吗? 我希望它的行为像它具有一定的质量和重量一样,并且它应该以逼真的方式对力和碰撞做出React。

When we work with 2D game design, we use Rigidbody2D, not Rigidbody. They're different in the sense that Rigidbody2D interacts only in the XY axis and overall, works in 2 dimensions. The Rigidbody 2D component overrides the Transform and updates it to a position/rotation defined by the Rigidbody 2D. This helps in adding velocities and accelerations to the object when needed.

在进行 2D游戏设计时,我们使用Rigidbody2D而不是Rigidbody 。 它们的区别在于Rigidbody2D仅在XY轴上相互作用,并且总体而言在2维上起作用。 刚体2D组件将覆盖变换并将其更新为刚体2D定义的位置/旋转。 这有助于在需要时为对象增加速度和加速度。

To further understand Rigidbodies, here's an example. Consider you have a normal cardboard box, shaped like a cube. Consider that the box itself doesn't have a defined weight for now, it's just a box that exists. If you wanted to move around the box, you could do it in a variety of different ways. Imagine that you want the cardboard box to behave like it's an actual, real world object with some mass. Let's say we filled it with sand. Then, we have a cardboard box that has actual mass, and feels heavy. If you pull or push it, you can expect to have to apply some effort before it moves. Rigidbodies act like the sand in this example. They add a sense of mass to the gameObject (that is, your cardboard box) so that it can interact with forces and other physics fundamentals like gravity and friction.

为了进一步了解刚体,这里有一个例子。 考虑您有一个普通的纸板箱,形状像一个立方体。 考虑一下盒子本身目前没有确定的重量,它只是一个存在的盒子。 如果您想在包装盒中四处移动,则可以采用多种不同的方式进行操作。 想象一下,您希望纸板箱的行为就像是一个具有一定质量的真实物体。 假设我们用沙子填充了它。 然后,我们得到一个具有实际质量并感觉很重的纸板箱。 如果您拉动或推动它,则可能需要在移动之前付出一些努力。 在此示例中,刚体的作用就像沙子 。 它们为游戏对象(即您的纸板箱)增加了质感,使其可以与力和其他物理原理(例如重力和摩擦)相互作用。

Enough about describing what a Rigidbody is, let's dig into the game. In the inspector for the gameObject, like Mr. Star, click on Add Component on the bottom. If you didn't modify the gameObject, the button should be just below the Sprite Renderer component.

足够描述刚体是什么了,让我们深入研究一下游戏。 在GameObject检查器中,例如Star先生,单击底部的“ 添加组件 ”。 如果您未修改gameObject,则该按钮应位于Sprite Renderer组件下方。

unity3d刚体_了解Unity 3D刚体



From there, go to Physics 2D → RigidBody2D. This will add a Rigidbody2D component to your gameObject.

从那里转到Physics 2D→RigidBody2D 。 这会将Rigidbody2D组件添加到您的gameObject中。

unity3d刚体_了解Unity 3D刚体

And a new row of RigidBody 2D features is added in the interaction view, just below the Sprite Renderer.

然后,在Sprite Renderer下方的交互视图中添加了新的RigidBody 2D功能行。

unity3d刚体_了解Unity 3D刚体



Let's run our game and see what happens. "Uh...What?" is probably what comes out of your mouth at this point. You'll notice that your gameObject drops like a rock! That's not really fun, is it?

让我们运行我们的游戏,看看会发生什么。 “呃...什么?” 此时可能是从您的嘴里出来的。 您会注意到您的gameObject像石头一样掉落! 那不是很有趣吗?

To fix that, simply set the Gravity Scale value of the Rigidbody's properties to 0.

要解决此问题,只需将刚体属性的“ 重力比例”值设置为0

unity3d刚体_了解Unity 3D刚体



Although there's nothing really different, is there? You had a sprite in the game before, and adding a Rigidbody and setting its gravity to zero seems to have brought us back to the exact same point.

尽管没有什么真正的不同,是吗? 您以前在游戏中有一个精灵,添加刚体并将其重力设为零似乎使我们回到了完全相同的点。

Well, not really. You see, by changing the gravity scale of that Rigidbody, you're basically defining how much the object is affected by gravity. In fact, try changing around the mass and gravity scale to different values and see what happens. The object still has a mass, it just doesn't care for the forces of gravity acting on it, and thus remains where it is.

好吧,不是真的。 您会看到,通过更改该刚体的重力比例,基本上可以定义物体受重力影响的程度。 实际上,尝试将质量和重力比例更改为不同的值,然后看看会发生什么。 物体仍然具有质量,它只是不在乎重力作用在其上,因此保持在原处。

Feel free to explore more options of Rigidbody by changing values of other attributes ans seeing its affects on the gameObiect.

可以通过更改其他属性的值来随意探索Rigidbody的更多选项,并查看其对gameObiect的影响。

翻译自: https://www.studytonight.com/game-development-in-2D/rigid-bodies

unity3d刚体