# Enemy NPC Patrol & Attack

Get help using Construct 2

### » Fri Feb 15, 2013 5:17 am

I've been trying to make my enemy NPCs patrol some sort of path and then attack the enemy if the player is in the LOS (line of sight).

To do this, I'm using an invisible box sprite pinned to each badguy so when you are in the box, the enemy starts chasing you. Once you're out of the box, the enemy goes back his scripted patrol in whatever location he stopped at.

The script I have is working, but only the patrol is working for 1 enemy. I'm wondering if anyone else has an easier way to do something like this?

My main event looks like this :

If box is overlapping player, attack
If box is NOT overlapping player, Bullet right, stop, bullet left, stop etc.

Any help or advice would be much appreciated.

Thanks,
Steve
B
23
S
3
Posts: 455
Reputation: 2,168

### » Fri Feb 15, 2013 5:46 am

Search the manual for the Distance expression. Basically it calculates the distance between 2 objects.

So if you are within a certain distance, perform an action.

B
96
S
29
G
11
Posts: 1,545
Reputation: 21,063

### » Fri Feb 15, 2013 6:02 am

If I were to use distance, how would I set the angle of which the enemy would start chasing? (ie. if the enemy is looking right, and you are in his scope of 270 degrees to 90 degrees, he chases, otherwise continues patrolling)?
B
23
S
3
Posts: 455
Reputation: 2,168

### » Fri Feb 15, 2013 7:42 am

Use the Within Angle condition. I.e. if player is within 45 degrees of self.angle
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

### » Fri Feb 15, 2013 9:02 am

Anytime I ever try to use an angle parameter, it never works. I might not be setting it up right, but if I have an NPC sprite on the screen and set it so if the player is within 45 degrees of BadGuy.Angle, how does it know what Angle the badguy is at when not moving?

B
23
S
3
Posts: 455
Reputation: 2,168

### » Fri Feb 15, 2013 3:01 pm

Facing direction and direction of movement are not always the same (consider "strafing", where a player moves sideways relative to the direction they are facing).

However, most of the built-in movement behaviours by default change the facing angle to match the angle of movement. Therefore, unless you've changed it to be otherwise, badguy.angle will be equal to the angle at which they last moved (even if they are currently stationary).
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

### » Fri Feb 15, 2013 7:02 pm

Ah - that makes sense. I believe I have changed it so the sprite itself does not rotate when moving.

With this is mind, can I still use the angle method, and if so - how?

Thanks so much!
B
23
S
3
Posts: 455
Reputation: 2,168

### » Sat Feb 16, 2013 12:43 am

Id love to hear any other suggestions. Im basically trying to implement zeldas npc behavior. Basically the npc will roam when not having spotted an enemy and has a pseudo field of vision based on the direction facing. So you're able to sneak oassed enemies.
B
23
S
3
Posts: 455
Reputation: 2,168

### » Sat Feb 16, 2013 12:46 am

I remember initially getting fet with calculating angles and distances and just resorted to using an actual vision cone as an invisible sprite that was pinned to enemies and calling all the actions based on an overlap or collision with the player.
B
50
S
14
G
3
Posts: 540
Reputation: 5,636

### » Sat Feb 16, 2013 1:59 am

That's what I had before trying some suggestions here, and it works great for 1 npc, but I found when I had multiple instances of BOTH the enemy sprite AND the invisible sprite, I couldn't figure out a way to link their actions appropriately. I used the For Each command everywhere, but it only seemed to work half the time depending on the action.

Mr E Bear - If you wouldn't mind, could you upload your capx for me to look at?

Thanks,
Steve
B
23
S
3
Posts: 455
Reputation: 2,168

Next