C# 我怎样才能让我流畅的第三人称动作停止颤抖?

C# 我怎样才能让我流畅的第三人称动作停止颤抖?,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,所以我已经被困在玩家移动上15个小时了。我的播放器在移动时会抖动。我注意到,如果: A) 我将CamPos代码放在与PlayerMovement代码相同的lateUpdate()函数中 B) 如果在“项目设置”中降低“时间”下的“固定时间步长”(使抖动不那么明显) 我想用正确的方法使我的播放器不会抖动。我的球员已经离开了,因为这会让他穿过墙壁。我使用rigidbody movePosition移动,因为我不知道如何使用rb.velocity使玩家移动到与摄影机相同的方向 我用相机做第三人称,用刚

所以我已经被困在玩家移动上15个小时了。我的播放器在移动时会抖动。我注意到,如果: A) 我将CamPos代码放在与PlayerMovement代码相同的lateUpdate()函数中 B) 如果在“项目设置”中降低“时间”下的“固定时间步长”(使抖动不那么明显) 我想用正确的方法使我的播放器不会抖动。我的球员已经离开了,因为这会让他穿过墙壁。我使用rigidbody movePosition移动,因为我不知道如何使用rb.velocity使玩家移动到与摄影机相同的方向

我用相机做第三人称,用刚体做动作。 以下是我当前在player PlayerMovement上的代码:

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

public class PlayerController : MonoBehaviour
{
    public float speed;

    public Rigidbody rb;

    public Transform camPos;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        camPos = Camera.main.transform;
    }

    private void FixedUpdate()
    {
        if (Input.GetKey("w"))
            rb.MovePosition(transform.position + (camPos.forward * speed));
        if (Input.GetKey("s"))
            rb.MovePosition(transform.position + (-camPos.forward * speed));
        if (Input.GetKey("d"))
            rb.MovePosition(transform.position + (camPos.right * speed));
        if (Input.GetKey("a"))
            rb.MovePosition(transform.position + (-camPos.right * speed));
    }
}

我也尝试过使用Vector3和输入轴来移动,但这会让玩家移动起来很笨拙(控件绑定到一个方向)。我还尝试使用cinemachine并关闭了相机上的所有脚本,以确保相机没有抖动。

刚体是游戏对象的物理表示。问题是,您没有直接使用
rb.MovePosition()
来使用游戏对象的速度/移动

但是很好!根据需要,您必须启用刚体“插值”以在帧之间创建平滑过渡

希望我帮了你,它会为你工作

编辑

  • 不要忘记将向量乘以时间。fixedDeltaTime in FixedUpdate()

太棒了。不知道移动位置是否是正确的移动方式。我一直想知道为什么在某些情况下你必须乘以时间。从未解释过。必须在Update()函数中使用Time.deltaTime,因为它对应于上一次Update()调用之间经过的时间。fixedDeltaTime与FixedUpdate()函数相同,fixedTimeDelta有助于消除抖动,但仍然会抖动。不过我还是很满意。
//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamPos : MonoBehaviour
{
    public Transform target;
    public float smoothTime = 0.5F;
    private Vector3 velocity = Vector3.zero;

    public GameObject player;

    private float mouseX, mouseY;
    public int mouseX_Speed, mouseY_Speed;
    
    //mouseSensitivity
    public int turnSpeed;

    void getMouseXY()
    {
        mouseX += Input.GetAxis("Mouse X") * smoothTime;
        mouseY -= Input.GetAxis("Mouse Y") * smoothTime;
    }



    void LateUpdate()
    {
        getMouseXY();
        // Define a target position above and behind the target transform
        Vector3 targetPosition = target.TransformPoint(new Vector3(0, 1.5f, -2.5f));

        // Smoothly move the camera towards that target position
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
        transform.LookAt(target);

        target.rotation = Quaternion.Euler((mouseY * mouseY_Speed), (mouseX * mouseX_Speed), 0);
        player.transform.rotation = Quaternion.Euler(0, (mouseX * turnSpeed), 0);

    }
}