通过刚体组件控制物体的移动
以下翻译主要引用自:http://www.ceeger.com/Components/class-Rigidbody.html,详情点击该网址。
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物理运算引擎和其他对象交互,任何游戏对象都需要包含一个刚体组件。
Property: | Function: |
---|---|
Mass 质量 | The mass of the object (in kilograms by default). 物体的质量(默认单位为kg) |
Drag 拉力(空气阻力) | How much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately. 受力运动时,多大阻力影响物体。0意味着没有,无穷大代表着使对象立刻停止运动。 |
Angular Drag 角阻力 | How much air resistance affects the object when rotating from torque. 0 means no air resistance.Note that you cannot make the object stop rotating just by setting its Angular Drag to infinity. 在扭矩作用下旋转时受到的空气阻力。0代表没有,注意:不能仅仅通过把该值设为无穷来使物体停止旋转。 |
Use Gravity 使用重力 | If enabled, the object is affected by gravity. 若开启,对象就会受重力因素的影响 |
Is Kinematic 满足运动学规范 | If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached. 若开启,对象将不受物理引擎的影响(但这不等同于没有刚体组件。这通常用于需要用动画控制的刚体,这样就不会因为惯性而影响动画了),只能通过改变它的Transform属性,来控制对象。这对可移动平台是很有用的,或者当你想要模拟刚体。(适用于模拟运动的平台或者模拟受铰链关节连接的刚体。) |
Interpolate 插值 | Try one of the options only if you are seeing jerkiness in your Rigidbody’s movement. 当你发现刚体运动时抖动,可以尝试该选项 |
- None | No Interpolation is applied. 不使用插值 |
-Interpolate 内插值 | Transform is smoothed based on the Transform of the previous frame. 基于上一帧的变换来平滑本帧变换。 |
-Extrapolate 外插值 | Transform is smoothed based on the estimated Transform of the next frame. 基于下一帧的预估变换来平滑本帧变换。 |
Collision Detection 碰撞检测 | Used to prevent fast moving objects from passing through other objects without detecting collisions. 碰撞检测模式。用于避免高速物体穿过其他物体,却未触发碰撞。 |
- Discrete 不连续、离散 | Use Discreet collision detection against all other colliders in the scene. Other colliders will use Discreet collision detection when testing for collision against it. Used for normal collisions (This is the default value). 不连续碰撞检测。使用不连续碰撞检测模式来与场景中其他碰撞器进行碰撞检测。其他物体与它的碰撞检测,也会应用这种模式。适用于普通碰撞(这是默认的模式)。 |
-Continuous 连续 | Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects) 连续碰撞检测。使用不连续碰撞检测来检测与动态碰撞器(刚体)的碰撞,使用连续碰撞检测来检测与静态网格(非刚体)的碰撞检测。采用连续动态碰撞检测模式的刚体碰见这类物体也将采用连续碰撞检测模式。而与其他刚体将采用不连续碰撞检测模式。这种模式适用于那些采用动态连续碰撞模式的物体碰撞的物体。(这对物理表现有很大的影响,如果你不关心与高速物体的碰撞,那么就让其为默认的不连续模式。) |
-Continuous Dynamic 动态连续 | Use continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects. 连续动态碰撞检测。使用连续动态碰撞检测模式来检测与连续模式和连续动态模式的物体间的碰撞。也适用于与静态网格(非刚体)的碰撞检测。而与之碰撞的其他模式的物体,采用的是不连续动态碰撞检测模式。适用于高速物体。 |
Constraints 约束 | Restrictions on the Rigidbody’s motion:- 对刚体运动的约束。 |
- Freeze Position 冻结位置 | Stops the Rigidbody moving in the world X, Y and Z axes selectively. 刚体在世界中沿所选X,Y,Z轴的移动,将无效。(选择哪个坐标轴,沿那个坐标轴的作用将无效) |
- Freeze Rotation 冻结旋转 | Stops the Rigidbody rotating around the world X, Y and Z axes selectively. 刚体在世界中沿所选的X,Y,Z轴的旋转,将无效。 |
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.
操作Transform和使用RigidBody之间的最大不同是力的使用。刚体可以接收外力和扭矩,但是变换不能。Transforms可以实现平移和旋转,但这和使用物理方式实现是不同的。当你亲自尝试的时候你会发现明显的区别。对刚体施加力和扭矩实际上改变的也是对象的Transform组件的位置和旋转。这就是为什么两种方式只能选择其一的原因。在使用物理运动方式的同时手动改变Transform可能会导致碰撞和计算的问题。(这就是为什么在变换和刚体间,最好只选择一种操纵方式的原因。同时使用两种方式会导致旋转及其他计算出现问题。)
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 menu. 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方法;来实现。 记住,不要同时使用物理与变换(选择直接修改transform时就不要使用Rigidbody等物理组件控制了)。
二.使用Rigidbody控制物体运动
1.这里主要记录的是通过AddForce()和AddTorque(),以及AddRelativeForce()和AddRelativeTorque()方法控制物体的移动和旋转。
2.通过该组件自带的方法AddForce()可实现物体沿任意方向的运动,但该种控制方式不会考虑被控制对象的自身状态,可能会导致物体的运动方向和物体的朝向不一致,比如:控制场景中的汽车运动,增加力使之向前运动时,物体向前运动了,但是汽车头却并不是向前(我们想要的是沿着汽车头的方向向前走)。这时可以使用AddRelativeForce()方法,施加力时它是以自身坐标系为基准,这可以保证力的方向与物体自身方向一致。
3.通过AddTorque()和AddRelativeTorque()是给物体增加一个旋转力矩,使之旋转。
4.通过设置刚体的velocity属性为0,可使物体停止移动
通过设置刚体的angularVelocit属性为0,可使物体停止旋转。
如下面所示:通过I,K,J,L和space按键实现物体的移动旋转和停止。
void Update () { //float moveHorizontal = Input.GetAxis ("Horizontal"); //float moveVertical = Input.GetAxis ("Vertical"); //Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical); //rb.AddForce (movement * speed); if(Input.GetKey(KeyCode.I)){ this.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3(-10f,0f,0f); } if(Input.GetKey(KeyCode.K)){ this.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3(10f,0f,0f)); } if(Input.GetKey(KeyCode.J)){ this.GetComponent<Rigidbody> ().AddTorque (new Vector3(0F,-20F,0f)); } if(Input.GetKey(KeyCode.I)){ this.GetComponent<Rigidbody> ().AddTorque (new Vector3(0F,20F,2f)); } if (Input.GetKey (KeyCode.Space)) { this.GetComponent<Rigidbody> ().velocity = new Vector3 (0f,0f,0f); } }