How would i raycast at an FOV on the x axis

Im making a first person game, and im making the raycast origin the players head, and i need a raycast for every degree in the fov (70 degrees by default/raycasts) so how would i do that?

I thought of some option but im not confident about them, maybe there’s a better way?

for i = -35, 35 do
	local direction = ???? (Thinking of the heads LookVector * distance of the ray, but how would i rotate the direction)
				
	local raycastResult = workspace:Raycast(head.Position, direction, raycastParams)	

	if raycastResult then
		print("Raycast hit!")
	end
end

I don’t know how to rotate it tho, i know i could do Head.CFrame * CFrame.Angles(0, math.rad(i), 0) but that returns as a CFrame not a vector and i wouldnt know where to put it in the direction variable, any help would be appreciated, btw here’s a sketch


also i think the degrees are right, correct me if im wrong

Why do you need that many raycasts?

im recreating the weeping angel effect aka scp 173, i need to see if the player is looking at him ig

Do you want “is on the player’s screen” or do you want “is the character vaguely facing towards”?

Wouldn’t it be the same since the game is in first person?

You could use a linear algebra method to detect that. Something like this detection right?

1 Like

Yeah something like that, but i would need more of the red “lines” for accuracy in first person

I’m not casting any rays here, I’ll show u the code for it 2 seconds

1 Like

I guess I missed the “in first person” part :slight_smile:

You could use this awesome method:

Or just call Camera:WorldToScreenPoint on the angel’s position as a rough approximation, then raycast toward it to check line of sight.

@DataSigh’s method of using Dot product will also work for a circular cone, but may not be perfect for detecting within a rectangle-shaped screen

1 Like

Welp im using a mesh so i guess i can’t do this, also thought about the cone approach but i’ll use it as my last option if i have to ill try the Camera:WorldToScreenPoint tho

If i did the Camera:WorldtoScreenPoint it would be on the client and i would have to fire a lot of remote events to see if the player sees the object or not, which is exploitable, any way to fix that?

It would be a good approximation still, and you could use one or two parts as the “visibility checker” proxy parts that were welded to the angle and invisible. You could also get the list of vertices of the mesh and use those in the script, but that’s more work.

One way or another you’re trusting the client. Like you could create a dummy camera on the server that you put at the same CFrame as a client’s and call the method on the dummy camera instead… but then how do you know the client isn’t sending you bogus camera CFrames? Ultimately roblox trusts the client implicitly because they handle their own physics. Not much you can do.

1 Like

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