大家好。我正在尝试在Unity中训练。我用多维数据集创建了一个空对象,其中包含火车应该乘坐的“变形”数据。一切工作都很好,但是我有这样的问题。坐火车几次“抽搐”时,我真的不明白为什么会这样。如果有人可以帮助您,我将很高兴听到您的答复。
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);
}
}
“扭曲”的示例:https://youtu.be/7xVMVrxbbgw
答案 0 :(得分:1)
如前所述,不应在FixedUpdate()循环中进行输入。那是用于物理计算的。
要解决此问题,我建议将这些Input检查从固定更新中移出,使节点之间的移动保持正常,然后将较大的IF块更改为Switch语句。在更新中执行所有操作效率很低。