(Solved) Enemy spotting Player

Hi There

I have a question about enemy behavior in a topdown game. The goal is: if the player is in the viewport of the monster (so the player is close to the monster + there are no walls/obstacles between them), the monster should follow to player. I tried to do this with rayracing, and it kind of worked.
The issue is: Not all monster instances behave independently. Only one instance reacts if the player is in sight and the other instances follow that one «mother» monster.

  • How can I fix this the simplest way?

  • By the way: if I close the Scene editor tab accidentally, how can I reopen it?

Thank you very much!

You can open the Scene editor by clicking here

And then the name of the scene

With your code, i think the enemy could shoot a invisible bullet in one direction, if the bullet collides with the player it moves the enemy in the direction of the player and delete the bullet, if it collides with a wall it delete the bullet.

1 Like

Hi, it seems you took the events from the “Basic AI with pathfinding”- example. But you missed an important part:

You need to use a ‘For each’-condition to make sure that the events are checked for all monsters.

That’s basically what the raycast is there for. No need to shoot invisible bullets.

Can raycast work like a cone? start from where object is and spawn until the max distance.

I have been working with invisible sprites that works like hitbox, attached to some point of an enemy and simulate a frontal visual, so if you are behind of enemy, it cant see you.

Can raycast do that? to avoid some lines on my actions

Yes, using 3+ raycast conditions pointing to different directions

As Reborn mentioned you need several raycasts for the outline of the cone and the area inside. I read somewhere that a lot of raycasts can be potentially problematic for the performance. Using an invisible sprite for simulating the field of view is often the easier and better method.


it will be like this?

oh i see, thats a good advice

Yes basically like that, but the angles of that picture are not quite accurate (in that case it would be
top-front: [ between -45 and -30 ],
front: [ 0 ],
bottom-front: [between 45 and 30 ]

You can use pre-determinated angles for a platformer game but if it is a top-down you should use the expression to get the angle of movement of the top-down behavior.

1 Like

It is posible to use just one view sprite of each instance of the same enemy?

If i trigger the collission with the “view” sprite the variable “chasing” is true and all enemies (that are the same sprite) start to chasing me instead of the one that i trigger.

Yes, that shouldn’t be a problem. You can use the “for each”-condition block to make sure that all enemies’ “chasing”-variable is switched.

1 Like

sorry for bothering again, so my idea is create the vision object on enemy and follow it.

here works, in some way, i can create the vision object for each instance of enemy
but i get infinite copies of the object, and it doesnt works with trigger once because only create the vision object on the first enemy.

‘Trigger once’ and the loop-conditions don’t work together. I would probably just create the vision-objects at the beginning of the scene and not bother to create and delete them based on the enemies’ distance to the player.
If you want to do this, though, you could just check the distance of enemy and player first (here you should add a Trigger once-condition) and then, as a subevent, you could add the loop for creating and deleting the objects (you would need it twice: for creating the field of view an for deleting it for all enemies).

1 Like

Thank you a lot! I took me a while to figure that out -.-

Yes, you’re right. It took the events from the “Basic AI with pathfinding”- example.
But I still don’t understand how I can set the ‘For each’-condition?

You can right-click on an event block → Add → Add For each object. Or you go to the upper right corner in the event editor and click the icon that shows a plus within a circle:

OMG, now it works perfectly!! thank you very much :slight_smile:

1 Like