Unity3d —— Rigidbody 通过刚体组件控制物体的运动

转自于 http://www.ceeger.com/Components/class-Rigidbody.html

刚体 Rigidbody

Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces via scripting, or interact with other objects through the NVIDIA PhysX physics engine.

刚体使物体能在物理控制下运动。刚体可通过接受力与扭矩,使物体像现实方式一样运动。任何物体想要受重力影响,受脚本施加的力的作用,或通过NVIDIA PhysX物理引擎来与其他物体交互,都必须包含一个刚体组件。

Properties 属性

在这里插入图片描述
在这里插入图片描述

Details 细节

Rigidbodies allow your GameObjects to act under control of the physics engine. This opens the gateway to realistic collisions, varied types of joints, and other very cool behaviors. Manipulating your GameObjects by adding forces to a Rigidbody creates a very different feel and look than adjusting the Transform Component directly. Generally, you shouldn’t manipulate the Rigidbody and the Transform of the same GameObject - only one or the other.

刚体让物体在物理引擎控制下运动。它可以通过真实碰撞来开门,实现各种类型的关节及其他很酷的行为。通过力来操纵物体,与直接通过变换不同,有一种不同的感觉。通常情况下,对同一物体,要么通过刚体操纵,要么通过变换操纵。

The biggest difference between manipulating the Transform versus the Rigidbody is the use of forces. Rigidbodies can receive forces and torque, but Transforms cannot. Transforms can be translated and rotated, but this is not the same as using physics. You’ll notice the distinct difference when you try it for yourself. Adding forces/torque to the Rigidbody will actually change the object’s position and rotation of the Transform component. This is why you should only be using one or the other. Changing the Transform while using physics could cause problems with collisions and other calculations.

通过变换与通过刚体操纵最大的不同在于使用了力。刚体相比于变换,能够接受力与扭力。变换能够控制平移和旋转,但与物理方式的实现不同。你可以通过实践来明显地区分它们的不同。给刚体施加力和力矩实际也会改变物体变换组件的位置与旋转角度。这就是为什么在变换和刚体间,最好只选择一种操纵方式的原因。同时使用两种方式会导致旋转及其他计算出现问题。

Rigidbodies must be explicitly added to your GameObject before they will be affected by the physics engine. You can add a Rigidbody to your selected object from Components->Physics->Rigidbody in the menubar. Now your object is physics-ready; it will fall under gravity and can receive forces via scripting, but you may need to add a Collider or a Joint to get it to behave exactly how you want.

刚体在受物理引擎影响之前,必须明确添加给物体。你可以通过选中物体,然后在菜单Components->Physics->Rigidbody来增加一个刚体组件。现在,你的物体的物理属性就设置好了,但你也可以根据需要来为其增加碰撞器或关节。

Parenting 父子级

When an object is under physics control, it moves semi-independently of the way its transform parents move. If you move any parents, they will pull the Rigidbody child along with them. However, the Rigidbodies will still fall down due to gravity and react to collision events.

当一个物体处于物理控制中,他会以半独立的方式随着变换的父亲的移动而移动。如果你移动父物体,那么它会将子刚体拖向它。但是,这个刚体仍然会根据重力下落或进行碰撞反应。

Scripting 脚本

To control your Rigidbodies, you will primarily use scripts to add forces or torque. You do this by calling AddForce() and AddTorque() on the object’s Rigidbody. Remember that you shouldn’t be directly altering the object’s Transform when you are using physics.

可以通过脚本增加力或力矩来控制你的刚体。通过调用刚体中的AddForce()和AddTorque方法。 记住,不要同时使用物理与变换。

Animation 动画

For some situations, mainly creating ragdoll effects, it is neccessary to switch control of the object between animations and physics. For this purpose Rigidbodies can be marked isKinematic. While the Rigidbody is marked isKinematic, it will not be affected by collisions, forces, or any other part of physX. This means that you will have to control the object by manipulating the Transform component directly. Kinematic Rigidbodies will affect other objects, but they themselves will not be affected by physics. For example, Joints which are attached to Kinematic objects will constrain any other Rigidbodies attached to them and Kinematic Rigidbodies will affect other Rigidbodies through collisions.

有些情况下,比如想实现布娃娃效果。需要刚体在动画与物理的操纵方式间切换。这种情况下,刚体可以标记为运动学模式。当刚体标记为运动学模式,他不会受到碰撞,力及任何物理影响,它表示你必须直接通过变换的方式来操纵该物体。运动学模式的刚体会与其他物体进行物理交互,但自身不受物理影响。例如,通过关节约束那些和运动学刚体连接起来的刚体,运动学刚体影响那些与之发生碰撞的刚体。

Colliders 碰撞器

Colliders are another kind of component that must be added alongside the Rigidbody in order to allow collisions to occur. If two Rigidbodies bump into each other, the physics engine will not calculate a collision unless both objects also have a Collider attached. Collider-less Rigidbodies will simply pass through each other during physics simulation.

碰撞器是另一种组件,它和刚体一起,来使碰撞发生。如果两个刚体撞在一起,物理引擎将不会计算碰撞除非它们包含一个碰撞器组件。没有碰撞器的刚体,会在物理模拟中相互穿透。
Add a Collider with the Component->Physics menu. View the Component Reference page of any individual Collider for more specific information:

通过菜单Component->Physics menu来添加一个碰撞器。想知道更多信息,请看参考手册中碰撞器方面的介绍。

Box Collider - primitive shape of a cube
盒碰撞器——基本形状是个盒子。
Sphere Collider - primitive shape of a sphere
球碰撞器——基本形状是个球。
Capsule Collider - primitive shape of a capsule
胶囊碰撞器——基本形状是个胶囊。
Mesh Collider - creates a collider from the object’s mesh, cannot collide with another Mesh Collider
网格碰撞器——从物体的网格创建一个碰撞器。不能与其他网格碰撞器相碰撞。
Wheel Collider - specifically for creating cars or other moving vehicles
轮碰撞器——特殊的碰撞器,用于创建车或其他移动交通工具。

Compound Colliders 复合碰撞器
Compound Colliders are combinations of primitive Colliders, collectively acting as a single Collider. They come in handy when you have a complex mesh to use in collisions but cannot use a Mesh Collider. To create a Compound Collider, create child objects of your colliding object, then add a primitive Collider to each child object. This allows you to position, rotate, and scale each Collider easily and independently of one another.

组合碰撞器是基本碰撞器的组合,共同扮演一个碰撞器的角色。当你有一个复杂网格却不适用网格碰撞器的情况下,使用组合碰撞器是个好的选择。要创建组合碰撞器,先为你的碰撞物体创建子对象,然后对每个子对象创建一个基本碰撞器。这样就允许你轻易的独立移动、旋转和放缩每个碰撞器。

In the above picture, the environment has a Mesh Collider attached. Mesh Colliders work the best for terrain or environments made from irregular shapes. The gun_model GameObject has a Rigidbody attached, and multiple primitive Colliders as child GameObjects. When the Rigidbody parent is moved around by forces, the child Colliders move along with it. The primitive Colliders will collide with the environment’s Mesh Collider, and the parent Rigidbody will alter the way it moves based on forces being applied to it and how its child Colliders interact with other Colliders in the Scene.

上图中,环境里包含一个网格碰撞器。网格碰撞器的最佳适用地方是不规则形状的地形或环境。如图,枪模型物体包含一个刚体组件,以及数个基本碰撞器的子物体。当刚体这个父对象受力移动时,它的子碰撞器也跟着移动。基本碰撞器与环境的网格碰撞器发生碰撞时,父对象的刚体也会因其子碰撞器与环境中其他碰撞器发生碰撞产生的力而改变运动轨迹 。

Mesh Colliders can’t normally collide with each other. If a Mesh Collider is marked as Convex, then it can collide with another Mesh Collider. The typical solution is to use primitive Colliders for any objects that move, and Mesh Colliders for static background objects.

网格碰撞器之间通常不相互碰撞,但如果一个网格碰撞器被标记为凸体,那么它就可以与其他网格碰撞器碰撞。典型的解决方案是,对移动的对象使用基本碰撞器,而对静态环境对象使用网格碰撞器。

Continuous Collision Detection 连续碰撞检测
Continuous collision detection is a feature to prevent fast-moving colliders from passing each other. This may happen when using normal (Discrete) collision detection, when an object is one side of a collider in one frame, and already passed the collider in the next frame. To solve this, you can enable continuous collision detection on the rigidbody of the fast-moving object. Set the collision detection mode to Continuous to prevent the rigidbody from passing through any static (ie, non-rigidbody) MeshColliders. Set it to Continuous Dynamic to also prevent the rigidbody from passing through any other supported rigidbodies with collision detection mode set to Continuous or Continuous Dynamic. Continuous collision detection is supported for Box-, Sphere- and CapsuleColliders. Note that continuous collision detection is intended as a safety net to catch collisions in cases where objects would otherwise pass through each other, but will not deliver physically accurate collision results, so you might still consider decreasing the fixed Time step value in the TimeManager inspector to make the simulation more precise, if you run into problems with fast moving objects.

连续碰撞检测的主要作用是避免高速物体的穿透。在不连续碰撞检测模式下,一个高速物体的上一帧位置在一个碰撞器的一边,而下一帧位置就穿透该碰撞器时,就发生了穿透。为了解决这个问题,你要为高速物体设置连续碰撞检测模式。设置连续碰撞检测模式将避免刚体穿透静态网格碰撞器(该网格未必是刚体)。而设置为连续动态碰撞检测将避免刚体穿透设为连续动态碰撞检测与动态碰撞检测的刚体。盒碰撞器、球碰撞器技胶囊碰撞器支持动态碰撞检测。主要,连续碰撞检测只是提供了一种穿透的保护,它保证捕获碰撞事件,但并不保证碰撞反应的精确性。所以,当你发现高速物体有这方面问题时,得考虑降低TimeManager中的固定时间间隔来让模拟更加的精确。

Use the right size 使用正确的大小

The size of the your GameObject’s mesh is much more important than the mass of the Rigidbody. If you find that your Rigidbody is not behaving exactly how you expect - it moves slowly, floats, or doesn’t collide correctly - consider adjusting the scale of your mesh asset. Unity’s default unit scale is 1 unit = 1 meter, so the scale of your imported mesh is maintained, and applied to physics calculations. For example, a crumbling skyscraper is going to fall apart very differently than a tower made of toy blocks, so objects of different sizes should be modeled to accurate scale.

你的物体网格的大小要比刚体的质量更加重要。如果你发现你的刚体不像你所期望的那样运动——慢、飘、碰撞不正确——那么请考虑调整你的网格资源的大小。Unity的默认长度单位是1单位=1米。你导入模型的大小会被保持,并参与物理运算中。例如,一个摩天大楼倒塌的表现肯定要异于一个玩具积木搭起来的塔,所以,对不同大小物体,要用精确的比例建模。

If you are modeling a human make sure he is around 2 meters tall in Unity. To check if your object has the right size compare it to the default cube. You can create a cube using GameObject->Create Other->Cube. The cube’s height will be exactly 1 meter, so your human should be twice as tall.

如果对一个人体建模,那么保证它大概在Unity中2米高。可以和一个默认的盒子对比来判断大小。使用 GameObject->Create Other->Cube来创建盒子。盒子的高为1米,所以你的人高度应该是它的两倍。

If you aren’t able to adjust the mesh itself, you can change the uniform scale of a particular mesh asset by selecting it in Project View and choosing Assets->Import Settings… from the menubar. Here, you can change the scale and re-import your mesh.

如果你不能自己调整网格,那你可以修改一个指定网格资源的比例,通过在项目视图中选中它,然后在目录里选择Assets->Import Settings,在这里,你可以选择比例,然后重新加载你的网格。

If your game requires that your GameObject needs to be instantiated at different scales, it is okay to adjust the values of your Transform’s scale axes. The downside is that the physics simulation must do more work at the time the object is instantiated, and could cause a performance drop in your game. This isn’t a terrible loss, but it is not as efficient as finalizing your scale with the other two options. Also keep in mind that non-uniform scales can create undesirable behaviors when Parenting is used. For these reasons it is always optimal to create your object at the correct scale in your modeling application.

如果物体在游戏中需要实例化成不同的比例,可以调整变换组件的比例轴。这种做法的缺点是在物体实例化时,物理模拟将执行更多操作,会导致游戏性能下降。 这不是很可怕的下降,但确实没有采用其他两种方法那样有效率。此外,父对象使用一个非标准比例的物体也会产生不好的行为。所以,建议是创建物体时尽量调整好标准比例。

Hints 提示

The relative Mass of two Rigidbodies determines how they react when they collide with each other.
两个物体的相对质量定义它们之间的碰撞反应表现。
Making one Rigidbody have greater Mass than another does not make it fall faster in free fall. Use Drag for that.
一个质量大的物体并不会在自由落体中下落更快。想实现这种现象请使用阻力。
A low Drag value makes an object seem heavy. A high one makes it seem light. Typical values for Drag are between .001 (solid block of metal) and 10 (feather).
阻力越大,物体看起来越轻,阻力越小,物体看起来越重。阻力的典型值在0.001(砖头)到10(羽毛)之间。
If you are directly manipulating the Transform component of your object but still want physics, attach a Rigidbody and make it Kinematic.
如你直接操纵一个物体的变换同时又想拥有物理特性,那么 为其增加一个刚体组件,并将其设为运动学模式。
If you are moving a GameObject through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.
若你通过变换来移动一个物体,并想接受碰撞或触发消息 ,你必须为其在移动时附加一个刚体组件。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值