Hey everybody. This is my first thread on the Unity forums. I’ve been using Unity for a couple of months now, and I’m working on a 3rd person shooter for Android. I’m happy to say that I’ve made a lot of progress, but I’m still going to wait to post my progress ![]()
For my game, I needed some decent pathfinding. Buying Unity Pro isn’t an option for me, so I designed my own
It works very well, and I have had no performance problems with it, even on mobile. It’s not completely finished, but it is very functional. And I’d like to know what you guys think.
Here is a Unity package containing the scripts.
1052621–39082–$navMesh.unitypackage (4.23 KB)
TUTORIAL:
Note: A path is returned as a LIST of Vector3’s. You must include “import System.Collections.Generic;” at the top of any object that uses pathfinding. A path is returned as type List.
-Add a mesh to your scene that you would like to use as the navmesh.
-Add the script “navMesh.js” to the navmesh object.
-In the inspector, click “Analyze navMesh”. (This may take some time, depending on the complexity of the mesh)
That’s all there is to setting up a navmesh. The navMesh.js script contains all of the functions that you need for finding paths. Here are a list of the functions and what they do.
-findPath(startPosition as Vector3, endPosition as Vector3)
Pretty straightforward. This function returns a path from point A to point B.
-findRandomPath(startPosition as Vector3)
This function returns a path from the start position to a random point on the navmesh. Useful for making characters wander around randomly.
-findRandomPathOfDistance(startPosition as Vector3, minimumDistance as float, maximumDistance as float)
This function returns a path from the start position to a random point that is between the minimum and maximum distance from the start position.
-drawPath(path as List., color as Color)
This will draw the given path in the editor. For debugging purposes.
Here is an example script that will make an object wander around a navmesh randomly. Add a navmesh and add this script to an object and watch it wander around.
#pragma strict
import System.Collections.Generic;
var navMeshScript : navMesh;
var path : List.<Vector3> = null;
var targetNodeIndex : int = 0;
var targetNode : Vector3;
var moveSpeed : float = 3.0;
var distanceToNode : float = 3.0;
function Start () {
//Find the navmesh script in the scene
navMeshScript = GameObject.FindObjectOfType(navMesh).GetComponent("navMesh");
}
function Update () {
//If the path is null
if (path == null) {
//Find a new random path
path = navMeshScript.findRandomPath(transform.position);
targetNodeIndex = 0;
}
//The end of the path hasn't been reached
if (targetNodeIndex < path.Count) {
targetNode = path[targetNodeIndex];
//If the character is close to the target node
if (Vector3.Distance(transform.position, targetNode) < distanceToNode) {
//Target the next node
targetNodeIndex += 1;
}
}
//The end of the path has been reached
else {
path = null;
}
//Move towards target node
transform.position += moveSpeed*(targetNode-transform.position).normalized*Time.deltaTime;
}