How do I Raycast against a wall? (Solved)

Hello everyone! Hope you all had a happy new year!

I have a question around Raycast. I’m making a topdown game, and I’m working on a feature where the enemies (enemy_1) is moving towards the player (player_body). However if the enemies (enemy_1)'s vision is blocked by wall (WallCornerFrontRight), then enemy will be idle.

I can’t seem to make this work, I’ve also read many post on the forum as well as this sample project, but my enemies’s Alert never become 0.

Am I doing something wrong? Any pointers will be amazing.

maybe a very basic question first: do you have your WallCornerFrontRight-sprite in the scene? It looks quite different (bricks) from the walls you have in your screenshot (wooden planks).

I am not 100% sure about that but your second sub event for the raycast is probably not necessary. I guess the state of your object variable Alert is 0 by default anyway? So if the raycast does not detect anything it can just stay as it is.

Hey thanks for taking a look!

The WallCornerFrontRight sprite is in the scene, I just duplicated 4 of them and stretched them to create a room to test :slight_smile:

I disabled the second sub event of the raycast, but the enemies are still moving for some reason. BTW I’m using the Boids movement rather than the pathfinder, does that affect raycasting?

I just realized where the problem is. If you check the linked example project, it says in the comment that the player and the wall object are both members of a group (solid). That’s what you also need to do: put your player and the wall in a group and test the raycast against it.

I have no experience with the boids movement and the linked example only uses the move-actions (no pathfinding) but there should be no problem with the raycast in either case.

You are right I needed to put both the player object and solid together into a group for it to work :slight_smile: Yay. And to confirm it works with boids too. Thanks again!

1 Like