Raycast has a range?

I am trying to write a script that checks how far something is in front of the player by using ray casting, but the ray will only hit objects within away stud from the player. My code is using the player’s humanoid’s MoveDirection for the ray’s direction, which I think is the issue because if I change the ray’s direction to something like Vector3.new(0, -100, 0) then it works perfectly.

function _runStep(deltaTime)
	local rayOrigin = humanoidRootPart.Position 
	local raycastParams = RaycastParams.new()
	
	local rayDirection = humanoid.MoveDirection
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	if (raycastResult) then
        local hitPart = raycastResult.Instance
		if (hitPart) then
		    -- Only returns objects within a stud of the player
        end
	end
end
_runService.RenderStepped:Connect(_runStep)

HumanoidRootPart is a Part. Parts don’t have MoveDirection. Humanoids do. I suggest using the below code instead if you want to check in front of the character:

local StudIncrement = 1000 -- the "range"
local rayDirection = humanoidRootPart.CFrame.LookVector * StudIncrement
3 Likes

Yeah, I type HumanoidRootPart by accident, but I changed it; anyway, this still works, thanks.

You only need this looped, otherwise you will have tons of RayCastParams.

local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	if (raycastResult) then
        local hitPart = raycastResult.Instance
		if (hitPart) then
		    -- Only returns objects within a stud of the player
        end
	end
1 Like

Yes; however, I do want the relative position, so I probably want the player’s position and move direction.

Don’t think you would need it as it’s stored in a variable and it just going to reuse it but go ahead