# Simple pathfinding issues

Get help using Construct 2

### » Sat Dec 07, 2013 6:15 pm

I want to be able to set up some waypoints in my project so when it's ran the enemies seem to wander the map to each of the waypoints so I can control where they can and can't go on the map.

But, I also want them to look as though they are wandering randomly around the map. They can change direction, but I only want them to be able to travel to the nearest waypoint.

Is there anyone that's had experience with this?

I've been using the pathfinding example included with construct 2, and while it's absolutely awesome, I can't really seem to get it to work with what I need currently.

Thanks guys!
B
4
S
1
Posts: 78
Reputation: 756

### » Mon Dec 09, 2013 1:46 pm

I've created my wandering NPC's in my zelda game by creating a sprite object called "pathsprite" and in each frame have images for the avialable directions (make sure you set the speed to 0 so it does not animate) for example:

frame 0 would have a image of an arrow pointing to the left
frame 1 would have a image of an arrow pointing to the right
frame 2 would have a image of an arrow pointing up
frame 3 would have a image of an arrow pointing down
frame 4 would have a image of an arrow pointing up,down,left and right
frame 5 would have a image of an arrow pointing to up and right

do this for all possible directions you require then set up logic like this

If player overlaps pathsprite and frame = 0 then set "NPC" movement direction to 180

If player overlaps pathsprite and frame = 1 then set "NPC" movement direction to 0

If player overlaps pathsprite and frame = 4 then set "NPC" movement direction to choose(0,90,180,270)

Also do a check position for when the NPC is (say) 4 pixels or less in distance then set NPC to the position to the pathsprite its overlapping. This ensures it goes throught the center of all pathsprites, you could also have the NPC wait at various places with a random timer to give the impression the npc is thinking were to go.

Hope that maks sence im at work so unable to provide screen shots.
B
14
S
6
G
3
Posts: 83
Reputation: 4,278

### » Wed Dec 11, 2013 7:33 pm

No, that definitely makes sense! Thanks a lot!
B
4
S
1
Posts: 78
Reputation: 756