How to see if something is within field of view

Hey guys.
I’m currently working on a wizard type game, and for that I need some magic wands that I need to make. Basically just like any other weapon system.

Anyways, I wanted to check if the point where a player clicks is within the player field of view/camera. So that an exploiter cant direct the shot somewhere else than where he can see (I know you can circumvent it, but still).

I found this function: Camera | Roblox Creator Documentation (WorldToViewportPoint). And if works perfectly for most of the stuff that I want to make, HOWEVER, when you shoot into the sky/at nothing it will return false. My guess is that you click on a place so far away (into the skybox) that it “technically” isn’t visible to the player. However, I can click it.

It’s hard to explain, but basically it gives you “false negatives” when shooting at the skybox, which I do not want. And therefore this function will not work for me, but I’ve heard that you can make a similar function using a :Dot product check.

I got this code snippet from an acquaintance:
mouse_position.Unit:Dot(camera.LookVector) >= math.cos(camera.FieldOfView)

As I said, I’m not good with these operators. So if anyone could see if it would work, or can give me an alternative it’s much appreciated!

Why not just put an invisible box around the playing area? I’ve used this solution when making a gun script.

I guess that would work. But it seems a little hacky so I’d rather get a way to find it out mathematically. Thanks nevertheless!

1 Like

the Dot function gives you a Scalar value between -1 and 1 depending if you’re looking toward or away from an object.

local facing = mouse_position.Unit:Dot(camera.LookVector)

Print the facing figure out when you click in each corner of the screen to see what your tolerances should be.
Although, I would also experiment with different display sizes / resolutions and FOVs as well.

If you need an example of how this works, you can use my local shadow render system as an example.
First Person Local Shadows - Resources / Community Resources - DevForum | Roblox

The code posted is on the right track, but id make some changes.

local vec = camera.CFrame.Position - targetPosition
if math.deg(math.acos(camera.CFrame.LookVector:Dot(vec.Unit))) >= camera.FieldOfView then 
    print('Not in view')
end

I’m not entirely sure if this works, but basically it gets the dot product of the camera’s forward vector and a vector facing the target position you want to check and gets the arcosine (basically converting it to radians) then converting it to degrees, which is then compared against the field of view.

edit: This is not a valid way of preventing exploits since its entirely client side. If you’re using a FPS system, you can check if the bullet is infront of the player but besides that this isnt a good method of securing something.

1 Like

Hey. Thanks for your answer - and sorry for my late response.

I didn’t get to test the code out, since I am unsure where to put it and how I would index the camera? If you could tell me that I would be very grateful, thanks.

Sincerely.

You need to put it in a localscript and the camera is just workspace.CurrentCamera