尝试在 Unity 中制作火车但遇到一些问题

Trying to make a train in Unity but having some problems

大家好。我正在尝试在 Unity 中制作火车。我创建了一个带有立方体的空对象,其中包含火车应该乘坐的变换数据。一切都很好,但我有这样的问题。坐火车 "twitches" 几次,我真的不明白为什么会这样。如果有人可以帮助我做什么,我会很高兴听到你的回答。

TrainMovement.cs

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

public class NewTrainMovemnt : MonoBehaviour 
{

    public GameObject waypointContainer;
    public AnimationCurve startCurve;
    private Vector3[] waypoints = new Vector3[30];
    public int currentWaypoint = 0;
    public float speed = 0.0F;
    public float currentSpeed;
    public int direction = 0;
    public int acceleration = 0;
    public float acc = 0;
    private float startTime;
    public Vector3 movementVector;
    public float damping;

    // Use this for initialization
    void Start () 
    {
        for (int i = 0; i < waypointContainer.transform.childCount; i++) 
        {
            waypoints[i] = waypointContainer.transform.GetChild(i).transform.position;
            Debug.Log(waypoints[i]);
        }
    }

    void FixedUpdate () 
    {
        if (Input.GetKeyDown ("e")) 
        {
            acceleration++;
        }

        if (Input.GetKeyDown ("q")) 
        {
            acceleration--;
        }

        if (acceleration==-1)
            acc=-0.001F;
        if (acceleration==-2)
            acc=-0.002F;
        if (acceleration==-3)
            acc=-0.003F;
        if (acceleration < -3) 
        {
            acceleration = -3;
        }
        if (acceleration==0)
            acc=0.0F;
        if (acceleration==1)
            acc=0.001F;
        if (acceleration==2)
            acc=0.002F;
        if (acceleration==3)
            acc=0.003F;
        if (acceleration > 3) 
        {
            acceleration = 3;
        }

        for (int i = 0; i < waypointContainer.transform.childCount; i++) 
        {
            MoveToWaypoint();
            startTime = Time.time + 2.0F;
        }
    }

    void MoveToWaypoint() 
    {
        currentSpeed += startCurve.Evaluate((Time.time - startTime)/10)*acc;

        float currentStep = currentSpeed * Time.smoothDeltaTime;

        movementVector = Vector3.MoveTowards(this.transform.position, waypoints[currentWaypoint], currentStep);
        Debug.Log(movementVector);
        float distance = Vector3.Distance(this.transform.position, movementVector);
        float distance_ = Vector3.Distance(this.transform.position, waypoints[currentWaypoint]);

        if (currentStep - distance > 0.001) 
        {
            currentWaypoint += direction;
            if (currentWaypoint >= 0 && currentWaypoint < waypointContainer.transform.childCount) 
            {
                movementVector = Vector3.MoveTowards(this.transform.position,waypoints[currentWaypoint], currentStep);
            }

            if (distance_ == 0f) 
            {
                currentWaypoint++;
            }
        }
    }


    // Update is called once per frame
    void Update () 
    {
        this.transform.position = movementVector;

        damping = 3.5f;

        Quaternion rotation = Quaternion.LookRotation(waypoints[currentWaypoint] - this.transform.position);
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, rotation, Time.deltaTime * damping);
    }
}

"twithcing"的例子:https://youtu.be/7xVMVrxbbgw

如前所述,不应在 FixedUpdate() 循环中完成输入。那是物理计算。

为了修复 Twitching,我建议将那些输入检查从固定更新中移出,Lerp 节点之间的移动,并将那个大的 IF 块更改为 Switch 语句。在更新中做所有这些是非常低效的。