I am new to pathfinding but i have read and watched through some articles, blogs and videos about Pathfinding.
Since those that i have read and watched from are mainly for RTS Games, Grid Based Pathfinding.
I couldnt find any tutorials where it explains in details of how you can make your own Node Based Pathfinding System.
So i hope, you guys could give me some insightful information to enlight me or even provide useful links which can help me.
I just want to make a very Basic Node Based Pathfinding system, and from that i hope to work from it to make it into a Pathfinding System suitable for my game.
Thank you in advance, you help is very much appreciated.
Its easy enough to use Unity`s built in NavMesh. so:
bake your nav mesh
create a gameObject and call it “Waypoint” or whatever (adding a gizmo to it is useful). Place them at intervals, creating a path you want you NPC to follow
add this script to your NPC and then drag all your waypoints into the waypoint list in the inspector
using UnityEngine;
using System.Collections;
public class PatrolTest : MonoBehaviour
{
private enum PatrolBehaviorState { MovingToNextPatrolPoint = 0 }
public Transform[] patrolPoints; //list of patrol points assigned to this gameobject
private PatrolBehaviorState patrolBehaviorState;
private int patrolPointIndex;
private bool enable = true;
private NavMeshAgent nav; //reference to the Nav mesh agent attached to this object
private Animator anim; //reference to the Animator attached to this object
private bool stopped;
public void Start()
{
anim = GetComponent<Animator>();
nav = GetComponent<NavMeshAgent>();
anim.SetFloat("Speed", 1f);
patrolPointIndex = 0; //set patrol points index to the first point
patrolBehaviorState = PatrolBehaviorState.MovingToNextPatrolPoint;
}
Vector3 GetCurrentDestination()
{
return patrolPoints[patrolPointIndex].position; //returns the current destination
}
void ChooseNextDestination() //chooses the next patrol point
{
patrolPointIndex++;
if (patrolPointIndex >= patrolPoints.GetLength(0)) //if we reached the last patrol point then go back to the first
{
patrolPointIndex = 0;
}
}
void UpdateMovingToNextPatrolPoint()
{
if ((GetCurrentDestination() - transform.position).magnitude < 1.5f) //check if we are close to the current assigned point
{
ChooseNextDestination(); //choose the next point to go to
var newRotation = Quaternion.LookRotation(GetCurrentDestination() - transform.position).eulerAngles;
newRotation.x = 0;
newRotation.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);
}
nav.SetDestination(GetCurrentDestination()); //set the Nav mesh agent on a path to the current destination
}
void Update()
{
if (patrolBehaviorState == PatrolBehaviorState.MovingToNextPatrolPoint)
{
UpdateMovingToNextPatrolPoint();
}
}
}
Almost certaintly all you have read about Grid Based Pathfinding can be extrapolated to Node Based Pathfinding. Each cell in a grid can be thought as a node.
One of the most standard algorithms is A* (A star), and is not so difficult to implement on your own if you are interested.