Question by
preiselbastian · Aug 29, 2018 at 08:44 AM ·
navmeshpathfinding
Pathfinding to waypoints using NavMesh
I want to make a simple script, that guides a NavMesh agent to various waypoints. I am new to Unity so I don't know some basic functions yet, which is instead typed in pseudo code. The reason I'm using NavMesh is because players can make obstacles for the pathfinding.
using UnityEngine;
using UnityEngine.AI;
public class Path_left_blue : MonoBehaviour {
private Transform target;
private int wavepointindex = 0;
public NavMeshAgent agent;
void Start () {
target = Waypoints_blue_left.waypoints[0];
}
void Update () {
//Set destination to waypoint
Vector3 dir = target.position;
agent.setDestination(dir);
if (agent is within a close range/touching target waypoint)
//Remove object if at the last waypoint
if (wavepointindex == Waypoints_blue_left.waypoints.Length)
Destroy(gameObject);
wavepointindex++;
target = Waypoints_blue_left.waypoints[wavepointindex];
}
}
Comment
You could just google for "unity navmesh agent waypoints" - and you'll get hits...
Also, see: https://unity3d.com/learn/tutorials/s/navigation
There's a patrol action IIRC.
Your answer
Follow this Question
Related Questions
Unity 2017.1 - Navmesh in 2D ? 2 Answers
[Unity .3.03f] NavMeshAgent stops after rebaking NavMeshSurface 0 Answers
Farthest Reachable Destination 0 Answers
Not optimal path on Nawmash 0 Answers