path finding/follow sprite best practise

Get help using Construct 2

Post » Sun Apr 02, 2017 1:37 am

Hey there,

Got a question regarding best way to implement a game mechanic. Basically, I want a separate AI (think cat or dog) to follow the player around the world, keeping a set distance from the player at all times. If the player starts moving in a different direction, I would prefer that the AI stays where it is and wait for the player to pass them, rather than running ahead to get behind the player. I would also want to be able to issue a halt/follow command to the AI, so it won't always be following the player but might need to catch up.

The game would be top down, so both X and Y need to be considered.

So I had a play around in C2 trying to get this running. At first I thought I could maybe run a bullet behaviour off the AI with it set towards an angle of player.X with an offset to sit it behind. But I can see this wouldn't work if there was a solid between player and AI.

Pathfinding sounds like it might be the answer, but I have a few issues regarding this. All the documentation on pathfinding states that it's system intensive. As the AI is going to need to be continually changing paths to follow the player, I can see that this might be a real technical issue, especially if the player has more than one AI following. There also seems to be an issue where pathfinding doesn't work well in the every tick event, which I assume is because it has to find a path before moving along it.

So, what I'm thinking now, is maybe I need to set up the pathfinding for it to run every X seconds as a way of lowering system load, but I'm still concerned that this isn't the best way to do it. I've been searching for some resources on this this morning, but either my Google powers are running out, or this question hasn't been covered. There were a couple of projects I found that I thought might be good to look at, but all the download links were broken.

What's the best practise for AI -> player pathfinding then? Has someone successfully achieved this using C2 and, if so, is there a project I can check out that demonstrates it?

Cheers
B
15
S
4
Posts: 38
Reputation: 951

Post » Sun Apr 02, 2017 2:27 am

There are tons of ways to implement it, but the first way that comes to mind for me is to use a trigger for pathfinding. If pet > certain distance from player, pathfind to player location, trigger once while true, only if not currently moving/pathfinding. On pathfinding complete, check again if in range of player or not. This way pathfinding only runs once when you get out of range, and only again if it reaches its destination and is still out of range. This would work best if you used a slight random offset from player as the target, or a larger cell size.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,615
Reputation: 61,651

Post » Sun Apr 02, 2017 2:36 am

And generally speaking if you're really worried about performance and the pet is cosmetic, it is probably perfectly acceptable for it to clip through walls and head straight for the player. The chances that this would occur should be relatively rare if you keep the leash distance short.
Mistakes were made.
B
52
S
26
G
109
Posts: 1,615
Reputation: 61,651

Post » Sun Apr 02, 2017 10:12 am

Message: LukeW can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Nah, they won't be cosmetic, major part of the game would be trying to keep them safe. And bullet behaviour won't work as, as I said, there will be a command to make them wait/follow so it will need to pick up the path from different positions.

I had a play around with it before and this is what I've got so far. Would love to hear feedback on better ways to do this if anyone feels like checking it out. Technically, it works, but I'd prefer to be able to do this without having to track the player facing position. Feel like there's a better solution to this and, as it's a core mechanic, I don't want to start off with something rubbish :lol:

www.dropbox.com/s/qrqf5jughdnv5i8/ai%20follow.capx?dl=0
B
15
S
4
Posts: 38
Reputation: 951

Post » Sun Apr 02, 2017 4:25 pm

Theres a game in development called fara and the eye of darkness (formally fara way). Anyway there was some animated .gifs posted some time ago showing how they did enemies chasing the main character which i think could be applied here. I can't find the link anymore, they may have removed them, but what they did was use line of sight and if it was clear then move enemies towards player, then when line of sight was lost the player would drop invisible sprite waypoints, the enemy would move towards the waypoint then check for line of sight again, if it found it it would move to the player again, if not it would move to next waypoint etc.
B
43
S
23
G
20
Posts: 735
Reputation: 12,027

Post » Sun Apr 02, 2017 11:20 pm

B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Apr 03, 2017 1:14 am

Awesome, thanks 99instances2Go. I'll have an in depth look at this later, but it looks pretty smooth.

Ethan, I considered LOS, but it's not something that I think fits my friendly ai. Will be something that I consider for the enemies though.

Cheers
B
15
S
4
Posts: 38
Reputation: 951

Post » Mon Apr 03, 2017 6:22 pm

Here's something similar to ethan's idea. Doesn't use any behaviors.
https://www.dropbox.com/s/a2cbp1qc04qd6 ... .capx?dl=0

It will remove/skip waypoints if a closer wp is found.
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Tue Apr 04, 2017 1:19 am

Cheers R0J0hound, that's really nice.

In terms of CPU performance, would using LOS be a better option over pathfinding? I'm feeling like the answer to that one is probably a yes, based on what I've read of the pathfinding behaviour but I wouldn't know.

While I'm only ever planning to have a maximum of three friendly instances following the player, it's something that I can see being easily transferred to enemy behaviour too.

I might run some tests with multiple instances all following the player and see what uses the most cpu.

Cheers
B
15
S
4
Posts: 38
Reputation: 951


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider] and 47 guests