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

Raycasting Bug? No output

Asked by
LevelKap 114
9 years ago

Rewrote the code for the wiki's laser gun tutorial.

local tool = script.Parent
local Player = game.Players.LocalPlayer
local Char = Player.Character
local Mouse = Player:GetMouse()
local start =  tool.Handle.CFrame.p   --where the ray originates
local lookAt = Mouse.hit.p - tool.Handle.CFrame.p  -- direction the ray is facing

tool.Activated:connect(function()
    local ray = Ray.new(start, (lookAt - start).unit * 300) -- creates the ray
    local tPart, pos = game.Workspace:FindPartOnRay(ray, Char) -- returns the part that it touched/intersected(tPart) and position of the part(tPart)

    local Hum = tPart and tPart.Parent and tPart.Parent:FindFirstChild("Humanoid")
    if Hum then
        Hum:TakeDamage(30)
    end

    local distance = (pos - tool.Handle.CFrame.p).magnitude
    local rayPart = Instance.new("Part",Char)
    rayPart.Name = "RayPart"
    rayPart.Transparency = 0.5
    rayPart.Anchored = true
    rayPart.CanCollide = false
    rayPart.TopSurface = 0
    rayPart.BottomSurface = 0
    rayPart.FormFactor = "Custom"
    rayPart.Size = Vector3.new(0.2,0.2,distance)
    rayPart.CFrame = CFrame.new(pos, tool.Handle.CFrame.p) * CFrame.new(0,0, -distance/2)
    game.Debris:AddItem(rayPart,0.1)
end)

the actual ray part seems to only be attatched to the floor on my game(not a baseplate). Wherever i move, the ray always points to a specific part on my floor. I'm not sure what's going on as there is no output.

Hierarchy:

        Players
                > LevelKap
                    > Backpack
                        > PaintballGun(tool)
                            >Local Script 
                            > Handle
                                > Mesh

Answer this question