Raycasting in-front of the player

I’m trying to raycast from the player’s torso in front to check if it hits anything. However, it ALWAYS prints no part!
I’m relatively new to raycasting so please bear with me.
This is a LocalScript in StarterPlayerScripts.

while task.wait(1) do
	local params = RaycastParams.new()
	params.IgnoreWater = true
	local wall = workspace:Raycast(script.Parent.Parent.Character.Torso.Position, script.Parent.Parent.Character.Torso.CFrame.LookVector, params)
	if wall ~= nil then
		print("found wall!")
	else
		print("no part!")
	end
end

How close are you to the part? It’ll only detect parts up to 1 stud away from the root’s centre. You probably need to make the ray longer by multiplying the direction by some number

I was extremely close to the part and yet it did not trigger.

local RAY_RANGE = 10 -- How many studs in front, editable

local params = RaycastParams.new() -- make params once
params.IgnoreWater = true

local character = script.Parent.Parent.Character -- define character as a variable

while true do
	local cframe = character:GetPivot() -- gets character cframe
	local result = workspace:Raycast(cframe.Position, cframe.LookVector * RAY_RANGE, params)
	if result then
		print("hit", result.Instance)
	else
		print("no part!")
	end
	task.wait(1)
end

4 Likes

I think just times where it says LookVector by your range as it will literally do like 0.01 in front of the player so x10 is usually big enough

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