那么,如果我想让多个不同的NPC每个都去他们自己的一套路标,什么是实现这一目标的好方法呢?
我有一个设置,其中有3组标记,waypoints 1-1,waypoints 2-1和waypoints 3-1,以及一个自动填充所有三组路径点的脚本。
但是,我将如何为每个可能的三个NPC指定哪个路径点,然后将其自动定位到下一个路径点呢?也就是说,NPC 1设置Waypoint2 1-1作为目标,NPC 2设置Waypoint2 2-1作为目标,然后我让它们移动到那里。到达后,他们分别将Waypoint1 1-2和Waypoint1 2-2作为目标。我不需要它们本身就可以找到路径,并且我认为transform.lookat应该可以工作;我更关心它们在指定的路径点之间的移动。
发布于 2022-07-31 00:21:22
从你提出问题的方式来看,我假设NPC 1要走到路点1,然后自动移动到路点2,最后移动到路点3,然后全国人民代表大会会重复这一点,在三角路径上有效地走来走去。
您可以这样做--购买创建脚本,让我们将其称为NPCMovementScript,并将其附加到每个NPC。
在脚本中,您可以有一个路径点列表,然后在NPC对象上调用MoveTowards以移动到每个路径点,我在下面的脚本中包含了一个示例:
public class NPCMovementScript : MonoBehaviour
{
public float speed = 1.0f;
public List<Transform> listOfWaypoints;
private int currentIndexOfWaypoint;
void Start()
{
currentIndexOfWaypoint = 0;
}
void Update()
{
if (Vector3.Distance(transform.position, listOfWaypoints[currentIndexOfWaypoint].position) < 0.001f)
{
currentIndexOfWaypoint++;
if(currentIndexOfWaypoint == listOfWaypoints.Count - 1)
{
currentIndexOfWaypoint = 0;
}
}
var step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, listOfWaypoints[currentIndexOfWaypoint].position, step);
}
}您必须在编辑器中按正确的顺序将路点对象拖放到列表中,从而填充路径点List<Transform>。
您也可能希望合并使用LookAt方法的建议。
希望这能有所帮助。
https://stackoverflow.com/questions/73178605
复制相似问题