Rigidbody移動時抖動問題
撞牆抖動
Unity中物體移動有非常多的方式;
比如:
transform.position += dir*speed*Time.deltaTime;
transform.Translate(pos, Space.World);
但是這種方式與碰撞結合時,是先位移在判斷碰撞,會導致撞牆抖動;
而鋼體中修改速度,或是添加力,是先判斷碰撞在移動,有效解決撞牆抖動問題;
RigidBody rig;
rig.AddForce(dir*speed);
rig.velocity = new Vector3(pos.x, 0, pos.z) * speed * Time.fixedDeltaTime;
眾所周知,人物移動時乘以Time.fixedDeltaTime,相當於邏輯在FixedUpade中調用,每間隔0.02s移動一次;
移動抖動
如果你的攝像機跟隨人物移動,一般會將攝像機邏輯寫在LateUpdate中處理,保證每一幀的最后調用,有效保護游戲的公平性;
但這樣鋼體移動和攝像機移動不同步,會導致移動時屏幕不斷抖動;
所謂這里的攝像機邏輯建議都寫在FixedUpdate周期中;
public class CameraController : MonoBehaviour
{
public Transform hero;
private float distacne = 6.5f;
private float height = 6.3f;
public void FixedUpdate()
{
transform.LookAt(hero);
transform.position = hero.position + new Vector3(distacne,height,0);
}
}
移動不下落
由於直接修改鋼體速度,速度y賦值為0會導致松了移動鍵物體才會下落,導致懸空走的情況;
解決方式,將鋼體速度的y值賦值為-0.3即可,模擬自由落體的重力;