Raycast inaccurate?

I’m having issues with Raycasting at the moment - I’m just messing around with it at the moment, however as you can see from the visual rods, it has moments of inaccuracy, this results in the location changing willy nilly etc.

I’ve attached the code below.

game:GetService("RunService").RenderStepped:Connect(function()
	local RCResult0
	local Origin, Direction
	if PseudoPart.Parent == Character then
		Origin, Direction = PseudoPart.Position, PseudoPart.Position - Vector3.new(0, 1000, 0)
		RCResult0 = workspace:Raycast(Origin, Direction, RaycastParam)
	else
		Origin, Direction = Camera.CFrame.p, Camera.CFrame.p - Vector3.new(0, 1000, 0)
		RCResult0 = workspace:Raycast(Origin, Direction - Vector3.new(0, 1000, 0), RaycastParam)
	end
	
	visualizeRay(Origin, Direction)
	
	if RCResult0 then
		if CurrentLoc == RCResult0.Instance.Parent then else
			CurrentLoc = RCResult0.Instance.Parent
			Render.Lighting(); ScreenGui:ClearAllChildren(); Render.Text()
		end
	else
		if CurrentLoc == Whitelist["Space"]["HitPart"].Parent then else
			CurrentLoc = Whitelist["Space"]["HitPart"].Parent
			Render.Lighting(); ScreenGui:ClearAllChildren()
		end
	end
end)

The second argument of WorldRoot:Raycast is a vector that is relative to the origin of the raycast, not a vector in the world space (relative to the world origin 0, 0, 0). Here’s what you can do:

if PseudoPart.Parent == Character then
    Origin, Direction = PseudoPart.Position, Vector3.new(0, -1000, 0)
else
    Origin, Direction = Camera.CFrame.Position, Vector3.new(0, -1000, 0)
end
1 Like

Will the cast the Ray directly down tho? As I’m looking to make it go 1k down from the Position instead of directly to 0, -1000, 0.

Yeah, that will make it raycast from origin in the direction -y for 1000 studs. The way you have it set up right now creates a vector from the position to 0, 1000, 0 which could be any direction depending on where the position is.