I want to create a tower defense game with unity. How do I set a predefined path for the enemies ai? On some forums, I read something about Pathfinding? What is it, and how do i learn it?
I understand most concept on Unity. The hardest part for me is scripting. I have never done anything involving with game development, but I do like the challenge of it
I see what you are saying. I was looking at Ant pathfinding tutorial scripts. It was so complicated for a beginner like me!! Maybe there is an easier way out there somewhere :roll:
If you’re just learning to program, I’d suggest leaving pathfinding alone at first. It’s probably the most difficult part of a tower defence game, in terms of algorithms. Just have your creeps go in a straight line for now, and by the time you’ve got the rest of your game in order, you’ll probably be up to implementing A* or using Path.
Ive setup simple pathfinding which would work in a game like this
basically I made a new class called pathFindPoints
place these as “wayPoints” in the editor.
have a static list that will contain them all
On Awake have the points add themself to the static list.
On Start find all connections thru the points
ie
foreach (pathFindPoint PFP1 in m_allPAthFindPoints)
{
foreach (pathFindPoint PFP2 in m_allPAthFindPoints)
{
if ( PFP1 can see PFP2 )
create a path link
}
}
This will setup all the links availalble
Then jsut work out which links are shortesst, or closest to follow.
This is a very simplist version of path finding but is probably fine for a tower defence.
Iin tower defense games you usually have one predefined path the enemies take, if your TD game will work so I don’t think you are looking for a pathfinding system but a way to make your enemies follow a predefined path, like the script xr1140 suggested.
I thought of this idea how to set a predefined path. Using the if and else statements in unity. Is it possible to make my creeps move the way I want it by using the transform.position functions?
Ex: If creep move to this coordinate
else then move to the next coordinate
While the creeps are moving to the set points, the walking/running animation will be playing during the whole time.
Is this a good alternative way to pathfinding? Or is it a bad programming style? :?
Well, I suggest you make a function named MoveToPoint, which will move the enemy towards a specific point, it will be inside a loop which checks if the enemy is near the point, if it is near, then move to the next point.
while ( Vector3.Distance (transform.position,target) > 5.0) {
transform.position += (target-transform.position).normalized*Time.deltaTime*speed;
}
target = nextPoint;
//Repeat the same thing
This is basically similar to the method I mention above.
Make a class/Game OBject called path find points. Place them in a static list at start of game.
That way you can easily in the editor position them where you feel is best and have as many as you want.
The enemy just traavel between them. once they get to one, head to the next one.
Yea sorry about that - I’ll be making a simpler interface for it “soon”
UnitySteer was the first step towards this - I’m looking at interfacing Path functionality to a simple component where you just specify a destination co-ordinate and don’t really have to worry about the path finding or following besides that.
A traditional pathfinding algorithm is actually a pretty poor match for TD pathfinding (that is, dozens of enemies all trying to get to the same point). You’ll do a LOT more calculating than is necessary; pathfinding in and of itself is already computationally expensive, and you’d be doing a pathfind for each and every creep.
A better way (and what any Desktop TD worth its salt uses, I’m betting) is a sort of “flood fill” of all the grid cells in the playfield. (google “flood fill algorithm” if you’re not sure how those work) Basically the idea is that each “cell” of the grid contains a pointer to the next cell a creep should go to, and you recursively “fill” these pointers in starting from the exit.
Start at the grid coordinates of the exit
From this point, check all four adjacent grid cells (or eight, if you want to allow diagonal movement); if no player tower is there, have this cell’s “next cell” pointer set to the cell you were just on.
Repeat this process, recursively, with each member the set of cells you just checked being the new “starting” cell. Make sure your algorithm doesn’t re-check cells whose pointers have already been set, or you’ll have infinite recursion on your hands.
When a creep needs to know which direction to go to next, he needs only to look at where his current cell is pointing. After you flood-fill the map with directions, the creeps are computationally free.
And yes, this is a reasonably advanced algorithm to write, but if you can, this is a much better way to pathfind on TD games. Not only is the algorithm itself at least as fast as a single pathfind operation, but you only have to do it once anytime the board changes.
Thx. For all the comments and tips!! My troubles aren’t over yet. My next trouble is learning how to grid my terrain! This will be :evil: for me! I won’t give up though…but it will take time to learn :x
For tower defence you dont even need to grid it. You can give each enemy a character controller to handle the movement and collisions. and path find points for direction. Its actually really simple.
The way I mention above also allows you to easily drop path find points into the editor/map so you can make pretty complex paths very easily.
From a beginners point of view, I have looked into A* pathfinding and it seems like the most straighforward way of adding directional AI to any style of game. But as an above comment said, there is not much point in delving into pathfinding untill you have the basics…
You should think about teaming up with someone, or even a few people! if you are more into the ideas and design of games then look for someone who can counter that with the programming skills.
If you use Aron Granbergs A* you have a nearly done solution out of the box for the path finding even if you do a maze type TD.
All you have to do is a 3 liner that whenever a tower is placed requests all currently walking around mobs to update their path and that only given your TD is a maze type TD where the user can build in the path, otherwise you don’t even need A* at all, cause for trivial point to point TD type (which can be very challenging too) with no new obstacles that can be put into the mobs paths, iTween + iTween Path Editor are sufficient already
The most complex part of a TD is not its programming anyway, most of the effort will go into good balancing of upgrades, tower power and challenging maps and wave designs. That means a massive amount of testing, retesting, developing some basic skills on statistical evaluation and potentially even outcome simulation right in excel in case you want to go commercial with it.
A TD is technically barely more complex than a good, powerup based breakout and breakouts are commonly seen as the “easiest game genre”, likely even easier thanks to Aron Granbergs great system which makes it a self solving topic to get the path and make mobs follow it.
A* is overkill for something like a traditional tower defense game, where every enemy spawns in the same spot and follows the same predetermined path of waypoints… assuming you don’t need obstacle avoidance. All you need is a public array in your script, and a bunch of waypoints in your scene, and drag them into the array in the inspector in the order you want the AI to follow them.
Then you would just have a counter initialize at zero, and rotate and move towards the counter position of the waypoints array(waypoints[0], waypoint[1], etc.), iterating it whenever you reach a waypoint, as long as the counter is less than the length of the array.
Here’s an example:
public var waypoints : Transform[];
public var moveSpeed : float = 5.0;
public var rotSpeed : float = 5.0;
private var currentWaypoint : int = 0;
function Update(){
if(currentWaypoint < waypoints.length){
RotateTowards(currentWaypoint.position, rotSpeed);
MoveForward(moveSpeed);
var waypointDistance = Vector3.Distance(transform.position, waypoints[currentWaypoint].position);
if(waypointDistance <= 0.5){
currentWaypoint++;
}
}
}
function RotateTowards (position : Vector3, speed : float) {
targetPos.y = transform.position.y;
var rotation = Quaternion.LookRotation(targetPos - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
}
function MoveForward(moveSpeed : float){
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}
Thats why I seperated the two cases with maze type TD, where no path exists, only start - end and the player is responsible for making a creative path by building towers (these TD are commonly called Maze).
For the case where you don’t do a maze type TD, I personally would use iTween and the iTween Path Editor, it can get barely easier as iTween even handles the move along path etc for you and the itween path editor gives you a visual tool to set it up. you can even easily do 2-3 path branches and make the code dynamically choose one on iTween, thats a 5 liner basically