Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does my raycast go backwards?

Asked by 5 years ago

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

1 answer

Log in to vote
0
Answered by
BloxRoxe 109
5 years ago

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

Ad

Answer this question