玩家在 运行 进入物体时摇晃

Player Jiggles When Running Into Object

我一直在尝试修复我的玩家碰撞脚本,但我不明白为什么。它应该阻止播放器 运行 穿墙,但是当我的播放器撞到墙上时,它会晃动一吨左右,就像脚本不够快 运行 一样。 (此代码来自 Sebastian Lagues 编码教程系列。)

脚本:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerCollision : MonoBehaviour
    {
    Rigidbody myRigidbody;
    public float speed = 5f;
    Vector3 velocity;
    // Start is called before the first frame update
    void Start()
    {
        myRigidbody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        Vector3 direction = input.normalized;
        velocity = direction * speed;
    }
    void FixedUpdate() {
        myRigidbody.position += velocity * Time.deltaTime;
    }
    void OnTriggerEnter(Collider triggerCollider) {
        print(triggerCollider.gameObject.name);

    }
}

当我在播放器上启用 Interpolate 并使用 MovePosition 时,它抖动得更多。

尝试将刚体的 collisionDetectionMode 设置为 Continous

我还会删除您的 velocity 变量并在 FixedUpdate 中设置 myRigidbody.velocity,如下所示:

void Update() {
}
void FixedUpdate() {
    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    Vector3 direction = input.normalized;
    myRigidbody.velocity = direction * speed;
}

https://docs.unity3d.com/ScriptReference/CollisionDetectionMode.html