Raycast not working

For some reason the raycast that I’m creating wont return anything. It’s always equal to nil and I have no idea why. Any help is appreciated.

function HammerFunctions:Follow(player,hammer)
	local pos = workspace.campos
	local mouse = player:GetMouse()
	RS.Heartbeat:Connect(function()
		local prams = RaycastParams.new()
		prams.FilterType = Enum.RaycastFilterType.Include
		prams.FilterDescendantsInstances = {workspace.Baseplate}
		local ray = workspace:Raycast(pos.Position,mouse.Hit.Position)
		if ray then
			print("hit")
			hammer.Position = Vector3.new(ray.Position.X,10,ray.Position.Z)
		else
			print("none")
		end
	end)
end

I am pretty sure the Recasting is working well. You have to remember your code only raycasts “Baseplate”. The line from pos.Position to mouse.Hit.Position should go through the Baseplate. Try printing out “Mouse.Hit.Position”. And try putting this code on top of your script:

mouse.TargetFilter = workspace.Baseplate
1 Like

You never passed in the ray params…
also your ray direction is supposed to be mouse.Hit.Position-rayOrigin

Also… Idk if you know this but if ray then will only be true if the ray hits something with a position, so if you shoot it at the sky it wont run. Idk if that was intentional or not

Also, you messed up with setting the actual position of the hammer

local ray = workspace:Raycast(pos.Position, Mouse.Hit.Position-pos.Position, prams)

hammer.Position = Vector3.new(ray.Instance.Position.X,10,ray.Instance.Position.Z)
1 Like

The second argument of workspace:Raycast takes a directional vector, you only provided a position meaning that the ray didn’t cast anywhere. You can instead use (pos.Position - mouse.Hit.Position) to fix this, as @SteveoAttano said. Also, you didn’t provide the RaycastParams in the workspace:Raycast.

1 Like

Huh. Well I’m an idiot lol. Thanks for the the help, it’s much appreciated! :slight_smile:

1 Like

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