1.Constraints
限制Rigidbody在某个方向轴上的移动和旋转。
脚本控制方法:
void Start ()
{
rigidbody.constraints =RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezePositionZ|rigidbody.constraints;
}
通过constraints属性来控制,这个属性的类型为RigidbodyConstraints,分为10种:
None | No constraints. |
---|---|
FreezePositionX | Freeze motion along the X-axis. |
FreezePositionY | Freeze motion along the Y-axis. |
FreezePositionZ | Freeze motion along the Z-axis. |
FreezeRotationX | Freeze rotation along the X-axis. |
FreezeRotationY | Freeze rotation along the Y-axis. |
FreezeRotationZ | Freeze rotation along the Z-axis. |
FreezePosition | Freeze motion along all axes. |
FreezeRotation | Freeze rotation along all axes. |
FreezeAll | Freeze rotation and motion along all axes. |
2.Collision Detection
一共有三种,默认为Discrete
要了解Collision Detection的作用,首先要了解什么是连续碰撞检测(Continuous Collision Detection)
连续碰撞检测(Continuous Collision Detection):
对于高速运动的物体来说,当Collision Detection为默认的Discrete时,有可能存在一种情形:前一帧时,物体在碰撞器的一边,下一帧时,物体已经穿越了碰撞器到达了另一边,以致于检测不到碰撞(OnCollisionEnter,OnCollisionStay,OnCollisionExit均不会被调用)。这时,我们就要进行连续碰撞检测,连续碰撞检测就不会存在上述问题,高速运动的物体不会相互穿透。
那么,如何才会进行连续碰撞检测?只有一下四种Collision Detection的组合会产生连续碰撞检测:
Continuous Dynamic(Rigidbody)+Continuous(Rigidbody)
Continuous Dynamic(Rigidbody)+Static MeshCollider(with out a rigidbody)
Continuous Dynamic(Rigidbody)+Continuous Dynamic(Rigidbody)
Continuous (Rigidbody)+Staic MeshCollider(with out a rigidbody)
以第一种来说,当碰撞的双方有一个Collision Detection设为Continuous,另一方设为Continuous Dynamic时,两者碰撞时,就会进行连续碰撞检测。
虽然有四种组合都可以产生连续碰撞检测,但是为了达到最佳效果,我们应该对高速运动的物体设置为CollisionDetectionMode.ContinuousDynamic,对这个高速运动的物体所要碰撞的物体设置为CollisionDetectionMode.Continuous.
但连续碰撞检测对物理性能有很大的影响,所以如果高速运动的物体的碰撞没有问题,应该设置为默认值CollisionDetectionMode.Discrete.
连续碰撞检测只支持使用Box,Sphere和Capusle碰撞器的刚体。
总结:
刚体和刚体之间的连续碰撞检测,刚体的碰撞器必须是Box,Sphere,Capusle.
刚体和非刚体(静态碰撞器)之间的连续碰撞检测,刚体的碰撞器必须是Box,Sphere,Capusle,非刚体的碰撞器必须是Mesh
注意:连续碰撞检测保证捕获碰撞事件以避免物体相互穿透,但并不保证碰撞结果(碰撞后该有的物理反应)的物理准确性,所以如果你在处理高速物体的碰撞有问题时,可以考虑到TimeManager中减小fixed Time step的值,来使物理模拟更精确。
3.Interpolate
默认为None
由于图形更新与物理更新并不同步,当图形更新比物理更新快得多时(可以在Edit->Project Settings->Time中将Fixed Timestep调得很大试试看),我们会发现物体会跳跃前进,这是因为当图形渲染了很多帧时,物理更新才完成了一次,在这期间物体都是不动的,所以我们会发现物体会跳跃式前进。
选择Interpolate模式时,物体会根据上一帧物体的位置进行平滑运动,总是落后一点点,但是比Extrapolate平滑。
选择Extrapolate模式时,物体会根据预估的下一帧物体的位置进行平滑运动。外插将基于当前速度来预测刚体的位置。如果有一个快速移动的物体,这可能导致刚体穿越碰撞器一帧,然后回弹。
插值默认是关闭的。通常刚体插值用于玩家角色(人物,赛车游戏中的车辆等)。物理是不连续的时间步运行,而显卡以可变的帧率渲染。这可能导致物体抖动,因为物理和显卡不完全同步。这个效果是细微的但是经常会在玩家角色上看到,尤其是如果相机跟随主角色。建议为(被摄像机跟随的)主角色打开插值,但其他的禁用。
4.Is Kinematic
使刚体不受物理引擎影响。
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. |
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.
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints. Eg. can connect a kinematic rigidbody to a normal rigidbody with a joint and the rigidbody will be constrained with the motion of the kinematic body. Kinematic rigidbodies are also particularly useful for making characters which are normally driven by an animation, but on certain events can be quickly turned into a ragdoll by setting isKinematic to false.