# Enemy Ai, capx attatched. Really struggling

0 favourites
• 19 posts
From the Asset Store
Give Sound to the enemies that are part of your game! :)
• Hello, Thanks for the help with this one. I have been at this for hours and getting really fed up with it.

Basically, when the player is 70 pixels or further, I want the zombie to walk to the player. Also when the player is to the right of the zombie, I want the zombie to walk to him.

I have an issue with the whole event,i have done something similar on a training game before and it worked. But not this time. The first capx is the game in question. The second cap was the game where it actually worked.

I have no clue what I am doing and trial and error is not working. I think this a knowledge gap or inexperience with events. Any help would be appreciated. Enemy AI is going to be a big part of the game, so I want to be doing things right in the first instance.

Ta

• One way is:

Check the distance int(distance(player.x,playery,zombie.x,zombie.y)). if it's less than 70, then you can do:

if the player.X is > than the zombie.X

- simulate control right

if the player X is < than the zombie.X

- simulate control left

• Thanks Ribis.

So I set that event first as a condition?

Is that an interger?

• One way is:

Check the distance int(distance(player.x,playery,zombie.x,zombie.y)). if it's less than 70, then you can do:

if the player.X is > than the zombie.X

- simulate control right

if the player X is < than the zombie.X

- simulate control left

Hi buddy, not sure why its not working, the events look ok?

• I think you haven't added the condition that checks the distance.

• as Xavier says, you need to add the condition of the distance before the state of the check of the < and > of the X.

I suggest you to make a variable into the zombie called "distance" so that you are able to decide for each enemy the right distance to activate them.

first you compare the distance, than compare if the X is > or <.

• Hi both, I am not sure. I have a variable of distance set up. And I think I have tired about 1000 variations of events with no joy.

Basically the zombie starts left of the player. When the player gets within a certain range (600 pixels) The zombies moves from idle to walk. This is ok, its working well - Cap attached.

But, If the player is to the right of the zombie, I am finding it nearly impossible to get the zombie to follow the player. Setting the player not mirrored wont work, because if the player turns hes going to mirrored non mirrored.

I dont know?

• Hello,

Have you tried line of sight, you could then have the zombie turn to the player and attack.

• Line of sight sounds good. Also on your screenshot you have done some weird picking, you should be simply saying if zombie is on left or right of the player, then move the zombie.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• condition

compare two variables,

distance(zombie.x,zombie.y,player.x,player.y) >= 70

sub event 1 condition

if zombie.x < player.x

action1 zombie set not mirrored

action2 zombie set x to self.x+5

sub event 2 condition

if zombie.x > player.x

action 1 zombie set mirrored

action 2 zombie set x to self.x-5

that's it.

if you want to limit where the zombie stops,

you just compare the

if zombie.x > player.x + zombie.width*2

respectively

if zombie.x < player.x - zombie.width*2

the 2nd option would make the zombie stop at a distance from player of around zombie.width*2 or if your zombie width is 35 pixels then it will stop at 70 pixels distance from the player.x which is given by the player origin point so if your player has the origin point in middle then it will calculate from that, if your player origin point is to left or right you will face a bit of problems visually, cause logically its still 70pixels distance.

• Hello,

Have you tried line of sight, you could then have the zombie turn to the player and attack.

Yeah I took LOS away because I think it was causing problems.

Is there an option for left and right?

I am just going to try corpstudio events and see how that goes. Not sure why I am struggling so much.

• condition

compare two variables,

distance(zombie.x,zombie.y,player.x,player.y) >= 70

sub event 1 condition

if zombie.x < player.x

action1 zombie set not mirrored

action2 zombie set x to self.x+5

sub event 2 condition

if zombie.x > player.x

action 1 zombie set mirrored

action 2 zombie set x to self.x-5

that's it.

if you want to limit where the zombie stops,

you just compare the

if zombie.x > player.x + zombie.width*2

respectively

if zombie.x < player.x - zombie.width*2

the 2nd option would make the zombie stop at a distance from player of around zombie.width*2 or if your zombie width is 35 pixels then it will stop at 70 pixels distance from the player.x which is given by the player origin point so if your player has the origin point in middle then it will calculate from that, if your player origin point is to left or right you will face a bit of problems visually, cause logically its still 70pixels distance.

Thank you Corp,Plinkie and Davy. Appreciate all the help. Worked perfect and the code looks a lot cleaner.

I should be able to work out the rest.

But what is this? self.x-5. The zombies self minus 5 pixels on the horizontal? Not sure why this works.

Thanks again. :-)

• But what is this? self.x-5. The zombies self minus 5 pixels on the horizontal? Not sure why this works.

Thanks again. :-)

self.x - 5 is basically the zombies current position and you subtract 5 from that making him change position again until it reaches the player.x (current position) + or - the zombie.width*2 that acts like a margin so they don't overlap.

now the 5 i put it there so its moving a bit faster, but u can adjust the 5 value to whatever u need, you can make a global variable and call it zombie speed and give it a value and replace the 5 with that variable. or if you have different zombies that require different speeds, you can make a localvariable on each zombie sprite, and give them different values, and then say

self.x - self.zombiespeed or self.x + self.zombiespeed

using variables is gonna be easier for you to manage the changes of values later on when events start piling up, so you don't have to go through each event, condition, action to look at that value 5 and say ok where is it now? XD

also what is distance> player.x and distance<player.x? is that the zombies X?

• also what is distance> player.x and distance<player.x? is that the zombies X?

I thought that ws needed to set the player distance?

I am really struggling to with the animations, if I try to make th player stop with the width animation, it kindda messes up the other animations. I might have to go back to basics here and learn about switching animations.

Thanks corp

• I'm not pretty sure whether I could be talking nonsense, but I think the issue is you are making player the object condition for the zombie animation.

If you say compare if player is X then zombie do something, runtime doesn't know which zombie to pick. If there is just one zombie at a time it doesn't matter, yet if there are more... That would be why it doesn't recognize right or left, for it gets confuses about which zombie to reference. The main event where the check sub-event comes from is even an everytick.

The expression for checking distance/position in the expression box is just a system comparison, either true or false. It is not picking the zombie from the expression, but the player. Be the condition referenced to the zombie object and then into the expression box, distance from zombie to the player, the right one would be picked. As to say:

- System Compare Zombie (expression: distance to player, position...) >

> Zombie set animation to Zombie.Animation

Forgive me in advanced if I am unnecessarily messing and you already took that into notice or solved by other means of picking. I only say becaise recently I had the same issue regarding path lines which didn't properly aim to their own troop for they were not correctly picked and was creating a domino-like chain effect from a fore event. After using line objects to check the events, rather than troop object, it all started to work right away.

• 19 posts