Raycast in a cone/triangle form?


I want to know how to use raycast to cast only in one direction and in a cone type of way. similar to how other games work:


So the enemies see in a somewhat realistic way!

Any ideas pls?

Hi, as far as I understand it, a raycast is just a line, it cannot have any specific shape. For a cone you could try to use several raycasts (for the borders of the cone and for the space between).
The most straightforward method is probably just to make a sprite in a cone shape and set it in collision with the object (e.g. players, enemies) you want to detect.


What Drona said is correct. You cannot have a cone-shaped raycast, but you can have a cone of rays.

I’d like to add to Drona’s post how I’d go with implementing that. I’d go with a sprite linked to each “enemy” object. Then it’d be something like this:
Player is in collision with FOVcone:
…raycast from enemy towards some group of raycast blockers, store the ray collision point in x and y
…position gotten from raycast touching or within player?: turn and run towards the player.

That is in fact almost the exact code I used in my puzzle game for the “zombie” enemy.
The player-visible cone you could probably make with lightning. Good luck!



Makes sense. Ty, I knew Raycast was a line but I didn’t put the two together :person_facepalming:

I used the sprite method previously and it worked pretty well. I just wanted to give a go at using minimal sprites, code, etc.

But I have learned that sometimes, cutting corners just brings about even more work.

So ty! All comments appreciated.

Ps. If anyone doesn’t know how to implement the sprite method or the raycast method. Feel free to ask and I, aswell as other members on the forum will try our best to explain.

1 Like

I’d imagine you could use the “object turned toward another” event

Idk…:person_shrugging: Im not very experienced in the raycast part of Gdevelop. Might work… Idk. But thanks :+1:

Do you really need a ray cast?

Here’s what I’d do instead
Create an object group call “SuspiciousObjects” and add all the objects that you want your enemy to “see”
For Each Enemy:
Enemy.DistanceTo(SuspiciousObjects) < Enemy.Variable(ViewDistance)
abs(Enemy.AngleTo(SuspiciousObjects) - Enemy.Angle()) < Enemy.Variable(ViewConeAngularWidth)

So that checks if there is a suspicious object nearby, then checks if the angle the enemy is facing is within a “view cone” of difference with respect to the angle from the Enemy to the object.