Firstly, I am unsure whether “repeat” is the right terminology (feel free to correct me on that). Secondly, I am making a waypoint-based navigation for NavMesh agent to follow. The problem is that I am using a timer (i.e WaitForSeconds) to assign the waypoint to the agent (if I don’t do this the loop will run just 1 frame and the agent just moves to the last waypoint).
Here is the pseudocode of what I was want to do:
if (agent reaches a waypoint)
{
agent.setDestination(nextWaypoint);
}
But I do not know how to do this.
Here is what I have so far:
IEnumerator wayPointPicker()
{
for (int i = 0; i < waypoint.wayPointPositionList.Length; i++)
{
opponentAgent.SetDestination(waypoint.wayPointPositionList*);*
yield return new WaitForSeconds(timer);*
}* } If you need more information to go on for I will try my best to supply. I will be very grateful if you can type a code snippet along with your explanation - preferably in C# but I can make do with JavaScript. Also if you have another method on tackling waypoints - feel free to share! I find techniques on solving the same problems fascinating! Thanks for taking the time to read this far!
I’m not entirely sure if the destination gets cleared when an agent reaches its destination, but if it does, a check of opponentAgent.HasDestination() could be used to check if they’ve made it to the waypoint.
Im not sure if this is what you are after, but I use the child’s/monkey’s approach, it seems (For a predefined path).
Here’s some snippets.
The first is the OnDrag routine which records my swipe across a RenderTexture from an overhead camera.
The Second is the simple timed stepping through the elements of the created List containing the points, and a Lerp to project my spaceship through the path.
Its pretty crude but you should be able to see how it works.
function OnDrag(data : PointerEventData)
{
if(enlarged)
{
didDrag = true;
var hitR : RaycastHit;
var maskL : LayerMask = 1 << 14;
var rayTouch : Ray = myCam.ScreenPointToRay(data.position);
if(Physics.Raycast(rayTouch, hitR, Mathf.Infinity, maskL))
{
this.playerPath.Add(Vector3(hitR.point.x, 10, hitR.point.z));
}
}
}