Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my raycast go backwards?

So, I've been making a gun that goes in the camera, and when you click, it's supposed to create a raycast going from barrel to the mouse's current position, if that makes sense. Every time I click, it creates the raycast, but it's going backwards.

https://imgur.com/a/mLEDRRS

Here is the code:

function rayCasting()
	local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p).unit * maxRange.Value)
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, rayCastIgnoreTable, false)
	
	if allowTracing.Value == true then
		local trace = Instance.new("Part", game.Workspace)
		trace.Material = Enum.Material.Neon
		trace.BrickColor = BrickColor.new("Black")
		trace.CanCollide = false
		trace.Anchored = true
		trace.Transparency = 0.5
		
		local distance = (hole.CFrame.p - position).magnitude
		trace.Size = Vector3.new(0.2,0.2, distance)
		trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0,0, -distance/2)
		print(distance)
		game:GetService("Debris"):AddItem(trace, 5)
		if hit then
			print(hit.Name)
		end
	end
end

2 comments

0

Your cursor is hitting a part closer to your camera than the starting point of the RayCast, causing it to go backwards.

0

Oh gosh, I feel so dumb now! Thank you!

1 answer

✓ Accepted answer

Your cursor is hitting a part closer to your camera than the starting point of the RayCast, causing it to go backwards.

This discussion is preserved as it was. New answers, comments, and votes are closed.