Best way to make line of sight detection for AIs?

I was brainstorming ideas as to how to go about making this, but I couldn’t think of anything yet.

Now before you say raycasting, I don’t believe that’s a good option. Because thereotically, that’d only work if the player is directly in front of the AI’s head (or where ever the ray is cast from.)

Ideally I can figure out an (efficient?) way to make a line-of-sight system, where almost like the AI has a cone of detection in front of their head, so if a player is found within that cone it’ll target the player. Rather than having the AI target any player nearby; not really what I’m going for.

Any help appreciated, thanks.

4 Likes

You could have multiple rays spread out, think of it like a 5x5 grid, and then you can limit the side of them, either that or use a part / mesh that uses a touch event, I really like the AI example this channel does, also heres a cool video on how he did helicopter AI Roblox Helicopter AI is Finally Here and it is DEADLY - YouTube

1 Like

Dot products combined with raycasts, best way I could think of.

2 Likes

I’ve used a simple combination of Camera:WorldToViewportPoint() and single raycast for players (not AIs), and it’s worked fine. It’s not true that the raycast only goes straight out from the player’s head, though. I cast from the player’s root to the other player’s root and if there’s nothing blocking the ray (the result is a descendant of the other player), it means the player is within line of sight. You could even get a bit advanced with it and ignore windows on your ray.

2 Likes

Some sort of cone projection that is similar to a human’s field of view? I was thinking about it the other day, what if they had artificial eyes, that glance around and focus on points of interest using a single raycast from each eye? You could position two parts inside the head and shoot the ray out then have the eyes use a sort of vector to glance around? It might give the illusion of sight.

1 Like

I think that raycasting is a good fit for this, but we need just 1 more thing to make it work. Assuming you want a cone of vision, you could check if the player’s HumanoidRootPart was within the cone of vision using something like this, and raycasting to the player’s HumanoidRootPart.

3 Likes