Totally at a loss on how to get this working halfway decently

Hey everyone,
I’m trying to get my enemy AI working halfway decently and so far it’s just been a mess. I’ve been reading these forums all day but I can’t seem to figure out how to make this work better.

For now all I’m trying to do is have a very basic enemy that chases the player around. I’m using a grid mesh that is generated at runtime since my levels are all generated at runtime. The grid looks fine… it seems to be mapping appropriately but there’s a whole lot that isn’t working…

First, I’m recalculating the path to the player every quarter second right now, which just seems like a bad idea but I couldn’t find any other way to have it work. This is the my enemy’s update code:

if(active) {

		if(Time.time - lastUpdate >= updateIncrement) {
				lastUpdate = Time.time;
				curPath = seeker.StartPath(transform.position, robot.position);
				curWaypoint = 1;
		if(curPath != null) {
			Vector3 dir = (curPath.vectorPath[curWaypoint]-transform.position).normalized;
			controller.Move(dir * 1.5f * Time.deltaTime);

			if(Vector3.Distance(transform.position, curPath.vectorPath[curWaypoint]) < 1) {
				if(curWaypoint >= curPath.vectorPath.Count - 1)
					curWaypoint = curPath.vectorPath.Count - 1;
				else curWaypoint++;


It creates a TON of these errors on any line that accesses the vectorPath[curWaypoint]:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index

And the other main issue I’m dealing with is the enemy will often walk up walls. If the path runs alongside a wall it just scales the wall, after which the path recalculates usually causing the enemy to spaz out because now it doesn’t know what the hell to do.

There’s a whole host of other things going wrong with it but I figure if someone can atleast point me in the right direction with these issues I might be able to figure out the rest. Any help would be much appreciated.


The main issue is that your script assumes that path calculations are instantaneous. But path calculations are queued and might not be calculated until after a few frames (this is for performance, to avoid large fps dips when requesting a lot of paths at the same time). Therefore, when you try to access vectorPath, it doesn’t exist yet so it will throw an exception. Also, you cannot know for sure that the path will even contain two points (which your script also assumes), if the character is standing right on top of the target, the path will only contain a single point.

Have you read the get started tutorial?

1 Like