Check if Ball is infront

Hi there, I am currently trying to make a Dodgeball game.

One of the fundimental parts of dodgeball as we all know is catching the ball.
I am trying to make it so only if the ball is infront of you you can catch it.

Ive tried using Dot, but this doesnt work as the balls LookVector could be different to your own whilst still being infront of you.

Heres what i’ve tried:

function DotCheck(Ball)
    local dot = Character.HumanoidRootPart.CFrame.LookVector:Dot(Ball.CFrame.LookVector) 
    if dot < 1 and dot > -1 then
        return true
    else
        return false
    end
end

When dot prints, depending on the side I touch the ball it is different. This is expected but i don’t know a way to not have it act this way or an alternative approach.

Instead of comparing against the ball’s look vector, compare against the unit vector from the player to the ball i.e. (Ball.Position - Character.HumanoidRootPart.Position).Unit

You also probably want your comparison to be something like

if dot > 0.9 then
  -- player facing ball
end

To debug you should use

print(tostring(dot))

and see what’s number is written for the next time

Thank you, master nicemike40. This is probably the 10th post you’ve helped me on.

did some printing of it i was just confused as to how i could fix it

Ok so, i tried a few ways of doing this and couldnt find one that worked.

Heres what ive been trying

   local dot = Character.HumanoidRootPart.Position.Unit:Dot(Ball.Position.Unit)
	print(dot)
	if dot > 0.9 then
		return true
	end

and also

	local dot = (Ball.Position - Character.HumanoidRootPart.Position).Unit
	print(dot)
	if dot > 0.9 then
		return true
	end
local toBall = (Ball.Position - Character.HumanoidRootPart.Position).Unit
local dot = Character.HumanoidRootPart.CFrame.LookVector:Dot(toBall)
return dot > 0.9