How do I make raycast not pass through walls?

I’m working on a game where an enemy can chase the player, but I can’t figure out how to not make the player see through walls to trigger a chase.

I have a raycast on the player that can trigger when enemy is in sight. However,
if there is a wall in the way, I don’t want the enemy to go into chase mode

Blue means that the enemy isn’t in sight of the player.

Red means that the enemy is starting to chase, with a wall in the way.

Here’s the event sheet.

Thank you very much. This is a bit complex for a beginner like myself, but I’ll use it as an opportunity to learn.

This version of that example is better

This is only event that uses raycast but i changed angle expression to be easier for you to understand

What is done here is that objects like all walls and obstacles are put into object group called solids
And from each IA (which is enemy tank) is cast ray into direction of player tank (perso)

If ray hits player instead of solids (walls)
Then variable Alerte of tanks (enemies is set to 1)

And they go into chase mode

I have been trying for a few hours to get it to work with your code, but my enemy still sees the player through the wall after he has intersected the raycast. I think I need to try and simplify the enemy.

Show me what you did

You need to have 3 (or more) objects - Player, Enemy and Wall.

Add Wall and Player to the one Object Group. Objects specified withing that group will oclude the ray.


Cast a ray for Enemy to Player against your group. Add a sub event to check if a ray hits a Player via “Currently picked instances” condition.

Raycast

1 Like

I get enemy to chase the player when in sight of the player. The problem is now that the enemy doesn’t stop pursuing the player, even behind walls.

1 Like

I think its very old example
I never even paid attention to its timers
BUT if i am right and it is so old then its from time when timers did not need to be started

Now they do

So try adding event on top of your events sheet

Condition
At beginning of the scene

Action
Start (or reset) the timer “AlertTimer”

This should fix your issue which is not your fault but example being old

1 Like

Now it works! 7ToGo had a simpler way of resolving this, but I know a future project that is going to need this type of raycast.

Thank you!

This actually works pretty well! Thank you :slight_smile: