Raycast help with example

I’m having trouble understanding how raycast works, could someone send me an example of a 2D platform where raycast works in these two ways?

1 = Enemy walking from side to side on top of a platform, using raycast to keep from falling off
2 = Enemy follow player if raycast collides with player

Currently in my game I’m using the “distance below”, but when the player is above the enemy, it keeps jumping. I believe that with raycast this can be resolved… But I don’t know how to implement it.

I would be very grateful if you could give me an example like that. Thanks!


and this too:

1 Like

I need to make some changes to my code and using raycast correctly. But I still can’t make it work as I would like.

As in the image, I need to make that when I touch the raycast beam on the wall, it comes back, and so whenever I see a wall in front of it.

Thus avoiding any collision with the wall.

I tried as in the first image, and also like this:

But nothing works.

However, when I was doing it for the player, this way it worked!

Why? Is there anything else on the raycast I need to know??

PS: obj_floor is also my wall, and floor, I use the same for both!

I suspect the issue is that you’re firing it at obj_floor. If there’s more than one obj_floor on the scene, it’ll choose [what seems] one at random. Do you need to raycast at obj_floor? Is there any reason why you can’t fire the raycast at a given angle?

Hi, I believe I found the error.

When I put the group of enemies (gEnemy) it doesn’t work, but on its own it works fine!

I don’t know if it has anything to do with this group, but it should work, right?

Believe me, just posting the problem here worked! I didn’t change anything at all, and as soon as I went to test it, it worked! :scream::scream::triumph:

1 Like