我如何使每个npc在延迟3秒后开始沿着路线点移动



这是附加到空GameObject:的航路点管理器脚本

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class WaypointsManager : MonoBehaviour
{
public GameObject npcPrefab;
public int numberOfNpcs;
public GameObject waypointsPrefab;
public List<GameObject> waypoints = new List<GameObject>();
public int numberOfWaypoints;
public bool useWaypointsPrefab = false;
private GameObject waypointObject;
// Start is called before the first frame update
void Awake()
{
for (int i = 0; i < numberOfWaypoints; i++)
{
if (useWaypointsPrefab)
{
waypointObject = Instantiate(npcPrefab, Vector3.zero, Quaternion.identity);
}
else
{
waypointObject = new GameObject();
}
waypointObject.tag = "Waypoint";
waypointObject.name = "Waypoint";
waypointObject.transform.position = new Vector3(Random.Range(0, 10), Random.Range(0, 10), Random.Range(0, 10));
waypoints.Add(waypointObject);
}
for (int i = 0; i < numberOfNpcs; i++)
{
if (npcPrefab != null)
{
GameObject npc = Instantiate(npcPrefab, Vector3.zero, Quaternion.identity);
}
}
}
// Update is called once per frame
void Update()
{

}
}

这个脚本附在每个npc:上

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public List<GameObject> waypoints = new List<GameObject>();
public float movementSpeed;
public float rotationSpeed;
public bool reverse = false;
public bool go = false;
public int numberOfWaypoints;
public int nextWaypointNumber;
private int waypointIndex = 0;
private GameObject nextWayPoint;
// Start is called before the first frame update
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("Waypoint").ToList();
numberOfWaypoints = waypoints.Count;
if (reverse)
{
waypointIndex = waypoints.Count - 1;
}
else
{
waypointIndex = 0;
}
StartCoroutine(MoveNpc());
}
// Update is called once per frame
void Update()
{
if (go)
{
if (reverse && waypointIndex == 0)
{
waypointIndex = waypoints.Count - 1;
}
if (reverse == false && waypointIndex == waypoints.Count)
{
waypointIndex = 0;
}
nextWayPoint = waypoints[waypointIndex];
nextWaypointNumber = waypointIndex;
transform.position = Vector3.MoveTowards(transform.position,
waypoints[waypointIndex].transform.position, Time.deltaTime * movementSpeed);
float distance = Vector3.Distance(transform.position, waypoints[waypointIndex].transform.position);
if (distance > 0f)
{
// Try to rotate to face the waypoint only if we're not on top of it.
var rotation = Quaternion.LookRotation(nextWayPoint.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
}
else
{
numberOfWaypoints--;
if (reverse)
{
waypointIndex--;
}
else
{
waypointIndex++;
}
}
}
}
private IEnumerator MoveNpc()
{
yield return new WaitForSeconds(3f);
go = true;
}
private void OnDrawGizmos()
{
if (waypoints != null)
{
for (int i = 0; i < waypoints.Count; i++)
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(waypoints[i].transform.position, 0.1f);
}
}
if (nextWayPoint != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, nextWayPoint.transform.position);
}
}
}

在Waypoints脚本中,我开始了一个延迟3秒的协同程序,但所有npc仍然像一个npc一样同时移动。我想让它等待3秒发送npc等待3秒钟发送npc,直到所有npc都沿着路点移动。

StartCoroutine(MoveNpc());

ISSUE
所有npc都在同一时间生成,每个npc等待3秒(所有npc同时生成(,然后开始移动。

解决方案
您可以创建一个需要等待一定时间的函数,并在协同程序中使用该延迟时间。

NPC

public void StartMovingAfterSeconds(float seconds) 
{
StartCoroutine(MoveNPC(seconds));
}
private IEnumerator MoveNpc(float delayTimeSeconds)
{
yield return new WaitForSeconds(delayTimeSeconds);
go = true;
}

当你生成npc时,你需要从你的经理那里跟踪延迟时间。

经理

// Amount to delay movement by (can be exposed in the inspector)
//
var delayTime = 3f;
// Accumulated delay
//
var currentDelay = 0f;
if (npcPrefab != null)
{
for (int i = 0; i < numberOfNpcs; i++)
{
var npc = Instantiate(npcPrefab, Vector3.zero, Quaternion.identity);

npc.StartMovingAfterSeconds(currentDelay);

currentDelay += delayTime;
}
}

我们从延迟0(currentDelay(开始,并在每次迭代中添加我们想要延迟的量(delayTime(。第一个延迟是0,接下来是3,然后是6,等等

最新更新