【发布时间】:2021-04-02 19:37:11
【问题描述】:
根据 Sebastian Lague 在 Youtube 上的播放列表,我最近完成了关于 A* 寻路的项目。但是,我相信他也是如此,我无法让寻求者(它们是胶囊)相互进入。当我给它们刚体时,它几乎起作用了,但随后它们开始到处传送并且通常非常有问题。
这是使搜索者遵循找到的最短路径的协程:
IEnumerator FollowPath()
{
bool followingPath = true;
int pathIndex = 0;
transform.LookAt(path.lookPoints[0]);
float speedPercent = 1;
while (followingPath)
{
Vector2 pos2D = new Vector2(transform.position.x, transform.position.z);
while (path.turnBoundaries[pathIndex].HasCrossedLine(pos2D))
{
if (pathIndex == path.finishLineIndex)
{
followingPath = false;
break;
}
else
{
pathIndex++;
}
}
if (followingPath)
{
if (pathIndex >= path.slowDownIndex && stoppingDst > 0)
{
speedPercent = Mathf.Clamp01(path.turnBoundaries[path.finishLineIndex].DistanceFromPoint(pos2D) / stoppingDst);
if (speedPercent < 0.01f)
{
followingPath = false;
}
}
Quaternion targetRotation = Quaternion.LookRotation(path.lookPoints[pathIndex] - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * turnSpeed);
transform.Translate(Vector3.forward * Time.deltaTime * speed * speedPercent, Space.Self);
}
yield return null;
}
}
让他们前进的是transform.Translate() 方法接近尾声。如果有人可以帮助我解决这个问题,我将不胜感激。如果我应该包含更多代码部分,请告诉我。
【问题讨论】: