Making a eye-sight system for ai

I want to make a similar ai that can actually see and detect people if it’s player or not.I’m currently making a The Mimic ai but I don’t want to raycast it infront since I’m new to ai making stuff.

15 Likes

there are multiple tutorials on youtube for this

9 Likes

Raycasting is really the only valid way to do this. It has a lot of benefits

  • performant
  • relatively easy to implement
  • works with walls
  • very customisable with properties like CanQuery and the RaycastParams paramater to raycast methods.
9 Likes

I’m actually thinking that I can’t just raycast it infront like just straight the player’s head,I want it to be like left and right too but if the player jump out of the raycast then it would bypass the vision.I want it to see the player inside the raycasting

9 Likes

Raycasting is not performant and there are better ways like using dot product. Example:

local PlayerPosition = -- players position
local HumanoidRootPartOfNPC = -- origin point that being the npcs position
local ToPlayer = (PlayerPosition-HumanoidRootPartOfNPC.Position).Unit
local LookVector = HumanoidRootPartOfNPC.CFrame.LookVector
local DotProduct = math.deg(math.acos(LookVector:Dot(ToPlayer)))
if DotProduct < 90 then
print("In Vision")
end
12 Likes

This has been answered multiple times in forum. For simplicity’s sake, I’ll link the one which would be most helpful.

12 Likes

This isn’t a valid way to do this because of walls. This does not factor into account that there are objects that could obscure the AI’s vision.
Also, this has seemingly infinite range too.
You could combine both the dot product method and raycasts as suggested above this method for a far more efficient method.

8 Likes

dunno where you heard that from, but roblox’s raycasting is actually very lightweight and safe to run in high-intensity scripts. you may be referring to the legacy raycasting, Ray.new(), rather than the modern workspace:Raycast()

6 Likes

Agreed, I was a little confused when I heard that so I did a bit more research and, yeah, you’re completely right.

7 Likes

add a cone shaped object in front of the npc that detects touched events is how i would go about this

7 Likes

instead of using touched events, you could use :GetPartsInPart() to get the parts in the cone-shaped area, but im confused as to why you cant just use :Dot()? its both more preformant and easier to manage.

7 Likes

No,what I want here is when you hide in some wall it can’t see you

4 Likes

Very useful!It did work fine and the result are really good since the ai is able to chase the player down now.Thank you for helping me!!

6 Likes

My aim is to give the OP a rough idea.

1 Like

Hm, well okay but as I stated there is a better more accurate way of detecting player within a specified FOV. And using raycast can be sometimes hectic, in my opinion.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.