Enemy only shoots when direct line of sight

Hi so I know how to get the enemy to point at the player and to shoot. But I need it so they only point at the player and shoot when they have a direct line of sight.

(all top down)
The player starts outside of a building and you can see an enemy inside, however right now it finds the player and starts shooting the moment I get too close. I would like it so it still checks for the player but really only reacts when the player has gone into the room where the enemy is.

I was thinking of doing with a logic gate and ray casting but im not sure how to extract the X and Y so it only outputs it to the angle? or I might be barking up the wrong tree completely.

Any help would be great thanks

2 Likes

You could have the enemy be checking for the player using proximity, then when the player is found, the enemy could start raycasting for the player. The proximity will also output the player’s x and y when detected so you can use that to point the enemy at the player.

I currently have it so always checking for the player and finds the player even when its outside the building.

But I don’t know how to convert the X and Y to just angle so that it can point in the right direction for the player. X and Y uses location where as the angle is just a max of 360.

Use the PointAt block

Then maybe lower the size of the proximity.

I currently do use point at block which is really useful.

As for reducing the size while i could do that it allows the player to get inside the building and just snipe at the enemy, and in fact the enemy itself is suppose to be a longer range enemy that is a bit harder to kill than the others.

Do you know of any way of maybe extracting the X and Y and just converting it to an angle?

I was playing around with adding another block that goes invis and painting where the door is so its almost like a painting line of sight, but its not working as well as I like.

Link here: Flowlab Game Creator - seeing player through walls

You could raycast once to see if there is a wall in front of the enemy, and if it misses, then it could cast to see the player?

1 Like

Just in case anyone wants to know how I was able to find a work around.

I used another block called line of sight and I made a proximity really small to find the player.

Once it found the player it would send its x and Y to a letter to the enemy called range
If it cant find the player (misses) it would send a message to the enemy called miss.

On the enemy itself I used a logic gate and used XNOR so that it only activates when both inputs are active. If that happens I made it so the enemy’s Proximity go to 800 which it can easily see the player, but when its not it just reverts to 250 which is just enough to detect if the player is in the room.

Im sure there are far smarter ways for this solution but this was the one I was able to come up with.

If people in the future think of a better solution please let me know as im always seeking out new ways to optimise the code.

You can see my workaround here: Flowlab - Cant see player through walls

2 Likes

Sounds like it’s just a Ray cast pointing towards the player.
Point at(angle) —> (angle)RayCast

Hit = player is in sight
Miss = player is not sight

1 Like

This is far better solution than what I came up with. I will say there needs to be a few extra steps to make it work the way I wanted:

First always get the enemy to always extract x and y from player. This goes into x and y on point at then this goes into angle in raycasting (as you mentioned) the cast length can be like 800 or whatever range the enemy is.

you then need a switch that are both off for x and y (put both into in) and have the raycast turn these on and off if it hits or misses the player, from the switches go into another point at which will rotate the sprite.

Finally make sure stop at first object is selected in raycasting (otherwise the enemy can still see you through walls)

Thank you CodeAlpaca! this really helped me out!

2 Likes