How do I make sprites run from player

Get help using Construct 2

Post » Fri Apr 15, 2016 9:14 pm

I was making a pac-man style game but I don't know how to make baddies run from me! Please help!
B
20
S
3
G
2
Posts: 94
Reputation: 2,402

Post » Fri Apr 15, 2016 9:44 pm

There are a few ways to do this, depending on your situation.
Do you require pathfinding, or are they free to run straight away from you?
I'm assuming you already have a system in place to have the baddies run towards you. How are you doing this?

Try having invisible "flee target" spots around the level. When they need to flee, pathfind to the one closest to them and farthest from the player. Or something along those lines. You'll probably need to add some conditions to ensure they don't flee towards a spot that the player is already along the path to.

You can use another invisible "avoid zone" around the player for that. When your baddie picks a spot to flee, draw a line between that spot and the baddie to see if it collides with the avoid zone. If it does, that means the player is in the way and it should probably go somewhere else.

Along the lines of the "avoid zone", you could probably update it as a solid in terms of pathfinding every once in a while, then the baddie using pathfinding definitely won't go there.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Sun Apr 17, 2016 12:30 am

@Programmer3000 I'm looking for the same thing.

@oosyrag I'm using "Set Angle Towards" to make baddies come at the player, but when my player changes into something different I want something like "Set Angle Away".
B
20
S
9
G
2
Posts: 15
Reputation: 2,599

Post » Sun Apr 17, 2016 1:08 am

For set angle towards method, that is the easy one. You can use Set Angle Towards player, then set Angle to Self.Angle+180 right after that.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Sun Apr 17, 2016 1:25 am

@programmer3000 Figured it out. Make your actions "Set Angle Towards(Player.X, Player.Y) then right underneath put "Rotate Clockwise" set at 180.

Edited to add...
@oosyrag I didn't see your answer. I like your answer better than mine. Thanks!
B
20
S
9
G
2
Posts: 15
Reputation: 2,599

Post » Sun Apr 17, 2016 1:35 am

Or better yet : just plain set angle to angle(player.x,player.y,enemy.x,enemy.y)
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Sun Apr 17, 2016 3:12 am

You could create an invisible sprite (I'll pretend it's called "InvisibleSprite") and put it at various points in the map. The more you put in the better because then when you want to get the enemies to run away from the player use an event like this:

By the way this is for a scenario like pacman, but if it was a bit more open world and you want them to run straight away from you there are much easier ways to do this.

Also make sure the enemies have pathfinder and give the invisible sprite an instance variable with initial value of 0... I'm going to pretend the instance variable is called "tracking".

Conditions:

System -> Every Tick

Player -> (whatever you use to indicate when the enemies will run away from the player)

InvisibleSprite -> Pick Nearest/Furthest -> Which: furthest
X: Player.X
Y: Player.Y



Actions:

InvisibleSprite -> Set Value -> Instance Variable: tracking
Value: 1



After this event make another that is like the following:

Conditions:

System -> Every Tick

Player -> (whatever you use to indicate when the enemies will run away from the player)



Actions

Enemy -> Find Path -> X: InvisibleSprite.tracking = 1 ? InvisibleSprite.X : Player.X
Y: InvisibleSprite.tracking = 1 ? InvisibleSprite.Y : Player.Y

InvisibleSprite -> Set Value -> Instance Variable: tracking
Value: InvisibleSprite.tracking = 1 ? 0 : Self.tracking




And finally do one more event like this:

Conditions:

Enemy -> On path found



Actions:

Enemy -> Move along path



That should do it, if it doesn't work either reply to this comment or private message me and I'll find another way to do it or figure out where you went wrong.

-Malcolmtheguy
B
11
S
3
Posts: 2
Reputation: 684


Return to How do I....?

Who is online

Users browsing this forum: draiza, matriax and 26 guests