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.
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
Your cursor is hitting a part closer to your camera than the starting point of the RayCast, causing it to go backwards.
Oh gosh, I feel so dumb now! Thank you!