Raycasting, but only in one direction?

Hello!

I’ve been recently trying to implement raycasting so enemies can detect the player, but only when the enemy is at front of the player (being behind their back doesn’t alert the enemy).

So, I’ve followed a tutorial on how to make raycasting, but now I have no idea how to make it so it only activates from one side??
(although I think I’ve got a bit of a workaround - would placing an invisible, uncollidable wall behind enemies back and including it into solid items group work? This way the raycast doesn’t go beyond enemies back)

Anyways, here’s what I have:

btw I have another problem with the stuff on the screenshot: when player dissapears from enemies sight enemy doesn’t stop acting alerted

In that code I don’t see a condition to make Alert = 0; something like a timer or maybe a sub event on the raycast for when number of player = 0.

For the raycast, you may use a way to calculate the way the enemy is looking. If it is side scroll, 0 and 180 for right and left. If you use top down view/pathfinding you may use the object rotation for the angle of the raycast.

1 Like

You’re right, I completely forgot about condition for changing into 0 value, thanks!

I just started working with raycasting, how can I calculate which way the enemy is looking? Should I just put in “180” in Ray Angle category?

Currently, I have this:
“ToDeg(atan2(Player.Y() - grasshopper_grunt.Y(), Player.X() - grasshopper_grunt.X()))”
I have no clue what it does? I just changed up some names

I use NPC.Variable(Direccion)*90, since I am working with 4 directions. Previously I do NPC.Variable(Direccion)=Int(NPC.Angle()/90).

If you are using all the directions I think you can use NPC.Angle() directly for the raycasting, since both are using degrees.

1 Like

Abut the function “ToDeg(atan2(Player.Y() - grasshopper_grunt.Y(), Player.X() - grasshopper_grunt.X()))” it calculates the difference between the player and the enemy Ys, then X’s, getting the inclination, not sure about the english word for it, then uses it as input for the inverse arc tangent of said differences, and lastly turns it into degrees…

I think you can use NPC:Angle() directly, if you have a top/down view.

1 Like

Thanks for explanation.

One last question: where can I learn the proper terms of calculating? I’m not sure if I have seen any documentation and I have no clue where some YT guy got things like “ToDeg(atan2”.
Does it use some other standard form of calculating?

It is trigonometry. I just remember the very basics, in youtube there is a lot of good tutorials

Ah, I meant terms more like “ToDeg” thing or meaning of “Int”

My bad, sorry. On the side button, (the blue with the 123) you can seek for Mathematical Tools, there you have the options for integer (int), cosine, sine, arctangent, etc.

1 Like