Free Navmesh Pathfinding

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 :wink:

For my game, I needed some decent pathfinding. Buying Unity Pro isn’t an option for me, so I designed my own :slight_smile: 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;
}

Thx man :smile: Sounds good :wink: I’ll try it :slight_smile:

Great job thanks for providing this as a free asset I’ll check it out!

I could use this. Would it be possible to analyze the the mesh during run time?

When I started working on it, I had it analyze the mesh when the game starts. But analyzing it is pretty slow, so it’s not really practical to do it during runtime. It has to iterate through each face, edge, and vertex a few times to determine neighboring faces and create connections between them. I can’t think of any way to determine that without iterating through everything several times

It looks cool so far.
You said it’s not completely finished. Will this be the case someday? (getting finished)

Yes. I wouldn’t consider it finished right now, but it’s very functional. I’m just adding onto it for my game as I see fit. If people started pushing me to work on it more, I might. But right now, my android game is my first priority because I hope to put it on the app market and make a profit

Could I ask what algorithm you’ve used? Also do you have any plans for making jumping features?

I used A*. And I don’t have any plans for that yet. If I were to add a new feature, it would be dynamic obstacle avoidance. I will probably have to add it at some point for my game, but it’s not necessary yet. The characters run into each other quite a bit right now haha

Great stuff. If I hadn’t done all my baking for my demo in my free trial I would definitely be using this!

I am really amateur in Unity, but I’m trying this A* Path Finding Project A* Pathfinding Project - A* Pathfinding Project, and the NavMesh components already built-in in Unity.
My question is: why you need to write your own path finding? Is NavMesh not enough? Why?
And, has someone tried the A* Path Finding package? Is it good?
Thanks,