Request Some Help With Raycasting

Alright so, I’ve been working on an AI for the past couple days. Where it moves when you don’t look at it, and also when the player “Blinks” (It’s an SCP-173)

I have it working well, but right now it just works on a timer. Haven’t implemented the blink mechanics yet. So right now I’m figuring out a solution to him being able to move when a player looks away/turns their back to him.

My current idea is to cast a ray in front of every player’s head, and check if the ray is touching the HumanoidRootPart of 173. If so, then it will wait on the blink time to hit 0, then he will advance towards the player.

All the necessary things such as killing the player and moving towards the player is done. I just need to have the control for his movement.

This is multiplayer, so my idea is that for each ray that’s touching him. It will add to the blink timer, to slow 173 down.

Questions:
1 - How should I cast the ray?
2 - How do I count the amount of rays that are touching the HumanoidRootPart? (In other words; how do I count the amount of players looking at the HumanoidRootPart?)

Any help would be appreciated, if you need any further explanation let me know.
This is my first post here, so yeah. Thank you for your time.

2 Likes

Using a ray is not a good idea here since vision is not a laser, it’s a field, so you’ll want to use the dot product to check if the player vision is looking at the general direction of the scp.

local plr: BasePart = workspace.Plr
local scp: BasePart = workspace.Scp

local angleInRadians = math.acos(math.clamp(plr.CFrame.LookVector:Dot((scp.Position - plr.Position).Unit), -1, 1))

if angleInRadians > math.pi / 2 then -- Human's FOV is about 180 degrees
	print("Not looking")
else
	print("Looking")
end
1 Like

My old AI for this did use :Dot(), the problem is that it would still work through walls. Otherwise I would use it again.

The original was mostly a joke game I used to learn AI more, but now I’m turning it into more of a serious game.

And this also doesn’t really help with the problem of counting the amount of players looking at him. Or at least you did not give a solution to that problem, but I do thank you!

1 Like

That’s easy enough to fix, only do the dot calculation when the ray you cast from the player to the scp doesn’t hit anything.

That’s also easy, inside the dot product, if the player is looking (the line that has print("Looking")), mark them as looking in CollectionService or any other way

2 Likes

Oh, Alright. I guess that would work, I’ll test it out when I get home tonight. Thank you for the help!

1 Like

its also possible to use

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint

but this would only be a good option if your game is in first person mode