Unity3D: where do we go from here

Finally doing something useful instead of promoting my game. Today is the day the foundation for a new project has been laid, and it started with pathfinding for my future virtual minions.

A* (pronounced “A star”) is a best-first pathfinding algorithm which produces least-cost optimal path from point A to point B, you can read everything about it on Wikipedia, but you don’t have to implement it yourself, not because I’m sharing my Unity3D C# static class with you, but because there’re probably a hundred of different implementations already.

Made a small vid showing how it works:

 
(Notice capsule moving diagonally in open unobstructed spaces – that’s a result of pretty simple path smoothing addition I made to classic A* implementation.)

I already used A* pathfinding algorithm in Railyard for building routes, but back then it didn’t account for obstacles and there was no path cost estimation. Now I finally made it something complete. My implementation uses spherecasting to check for obstacles in a way characters won’t hit corners when traversing the path. (Make sure your obstacles are on “Obstacle” layer and they all have non-trigger Collider on them.)

Now, open that gist, copypaste into your project and use AStar.FindPath() method, supplied with two Vector3’s to find best path between them. You want smooth, non-“pixelated” path like in the video? Use AStar.SmoothPath() with a path from first method to get it smooth. You’ll probably need to adjust some values to be in the scale of your game (I use 1.0f scale everywhere, mind that), so read comments in the class, there’s enough info for you to understand what’s under the hood.

Go now, you can thank me later.

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: