0 Favourites

# Enemy NPC Patrol & Attack

• 11 posts
• 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

• 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.

• 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)?

• Use the Within Angle condition. I.e. if player is within 45 degrees of self.angle

• 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?

• 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).

• 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!

• 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.

• 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.

• 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

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Miraculously I figured out a way to do this. This is the event setup I have:

<img src="http://stevemazzaro.com/Patrol1.png" border="0" />

With the Patrol function being:

<img src="http://stevemazzaro.com/Patrolfunction.png" border="0" />

This works relatively well, but for some reason after they chase me a bit, if I am out of the red box, they will stop, but after running in and out of the box, eventually they stop patrolling altogether. I can't figure out what it is I do that stops them from Patrolling, but the second I'm on the red box, they're back to chasing me.

Can anyone see what's wrong with this setup?

Also another small issue, occasionally they seem to stutter a bit on the patrol function, but not always.

• 11 posts