首先,很抱歉如果这篇文章写得不太好,我已经花了几个小时来调试它,感到非常有压力。我正在尝试在Unity中制作一个可以在路标之间移动的运动平台,但我不想在世界中放置大量的游戏对象占用宝贵的处理能力,因此我正在尝试使用一些可以通过编辑器添加到脚本中的东西。
唯一的问题是它似乎以难以置信的速度进行这项工作:
黑色=相机视图,蓝色=基于路点移动的平台和它应该去的地方,红色=当前正在做什么。
我花了几个小时来找到解决方法,但我不知道为什么它会这样做。
我的平台脚本:
唯一的问题是它似乎以难以置信的速度进行这项工作:
![enter image description here](https://istack.dev59.com/Z1fj8.webp)
我花了几个小时来找到解决方法,但我不知道为什么它会这样做。
我的平台脚本:
public Vector3[] localWaypoints;
Vector3[] globalWaypoints;
public float speed;
public bool cyclic;
public float waitTime;
[Range(0, 2)]
public float easeAmount;
int fromWaypointIndex;
float percentBetweenWaypoints;
float nextMoveTime;
void Start()
{
globalWaypoints = new Vector3[localWaypoints.Length];
for (int i = 0; i < localWaypoints.Length; i++)
{
globalWaypoints[i] = localWaypoints[i] + transform.position;
}
}
void Update()
{
Vector3 velocity = CalculatePlatformMovement();
transform.Translate(velocity);
}
float Ease(float x)
{
float a = easeAmount + 1;
return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a));
}
Vector3 CalculatePlatformMovement()
{
if (Time.time < nextMoveTime)
{
return Vector3.zero;
}
fromWaypointIndex %= globalWaypoints.Length;
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);
Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);
if (percentBetweenWaypoints >= 1)
{
percentBetweenWaypoints = 0;
fromWaypointIndex++;
if (!cyclic)
{
if (fromWaypointIndex >= globalWaypoints.Length - 1)
{
fromWaypointIndex = 0;
System.Array.Reverse(globalWaypoints);
}
}
nextMoveTime = Time.time + waitTime;
}
return newPos - transform.position;
}
struct PassengerMovement
{
public Transform transform;
public Vector3 velocity;
public bool standingOnPlatform;
public bool moveBeforePlatform;
public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform)
{
transform = _transform;
velocity = _velocity;
standingOnPlatform = _standingOnPlatform;
moveBeforePlatform = _moveBeforePlatform;
}
}
void OnDrawGizmos()
{
if (localWaypoints != null)
{
Gizmos.color = Color.red;
float size = .3f;
for (int i = 0; i < localWaypoints.Length; i++)
{
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
}
}
}
更新: 经过进一步测试,我发现如果我本地航点数组中的第一个对象设置为0,0,0,而我的第二个对象设置为1,0,0,则平台将向右螺旋,确保在螺旋时击中航点,然后像上图一样螺旋到无处。但是,如果我将第一个对象设置为0,0,0,将第二个对象设置为-1,0,0,则对象将像以前一样操作,但是将像此图所示向左螺旋。(第二张图片也已更新,显示了平台如何确保在螺旋到无处之前击中两个航点)。
我还注意到,如果我将两个航点都设置为0,0,0,则平台保持静止,这两件事证明它与处理航点的方式有关,而不是其他脚本或父对象的干扰。