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

How would you make a laser point from tool.Fire1 to game.workspace.Camera.MeatLoaf?

Asked by 8 years ago

How would you make a laser point from tool.Fire1 to game.workspace.Camera.MeatLoaf

Code:

local laserpos = game.ServerStorage.MeatLoaf:Clone()
laserpos.Parent = game.Workspace.Camera
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local laser = game.ServerStorage.Laser:Clone()
laser.Parent = game.Workspace.Camera
mouse.TargetFilter = game.Workspace.Camera

local clicked = false

tool.Equipped:connect(function(mouse)

    mouse.Button1Down:connect(function()
        clicked = true
    end)

    mouse.Button1Up:connect(function()
        clicked = false
        tool.Fire1.BrickColor=BrickColor.Black()
        game.Workspace.Camera.MeatLoaf.Position = game.Workspace.ss.Position
        game.Workspace.Camera.Laser.Position = game.Workspace.ss.Position
    end)

end)

while wait(0.05) do
    if clicked then
    local mouse = game.Players.LocalPlayer:GetMouse()
      tool.Fire1.BrickColor=BrickColor.Red()
local LLX = ((tool.Fire1.Position.X-game.Workspace.Camera.MeatLoaf.Position.X)/2) + game.Workspace.Camera.MeatLoaf.Position.X
local LLZ = ((tool.Fire1.Position.Z-game.Workspace.Camera.MeatLoaf.Position.Z)/2) + game.Workspace.Camera.MeatLoaf.Position.Z
local LLY = ((tool.Fire1.Position.Y-game.Workspace.Camera.MeatLoaf.Position.Y)/2) + game.Workspace.Camera.MeatLoaf.Position.Y
local LL = math.sqrt(LLX^2+LLZ^2+LLY^2)

        game.Workspace.Camera.Laser.Size=Vector3.new(LL,0.5,0.5)
        game.Workspace.Camera.Laser.Position=((tool.Fire1.Position-game.Workspace.Camera.MeatLoaf.Position)/2) + game.Workspace.Camera.MeatLoaf.Position
        game.Workspace.Camera.MeatLoaf.Position=mouse.Hit.p



        wait(0.05)


    end
end

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

Your problem was how you were going about drawing the laser. You were trying to do way, way, way more than you have to.

Essentials of drawing a laser:

  • Get distance between points
  • Size according to distance
  • CFrame.new(origin,look) * CFrame.new(0,0,-distance)
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cam = workspace.CurrentCamera
local tool = script.Parent
local fire = tool.Fire1
local ss = game.ServerStorage
local laserpos = ss.MeatLoaf:Clone()
local laser = ss.Laser:Clone()
local clicked = false

laser.Parent,laserpos.Parent,mouse.TargetFilter = cam,cam,cam

tool.Equipped:connect(function(mouse)
    local _ss = workspace.ss;
    mouse.Button1Down:connect(function()
        clicked = true
    laser.Transparency = 0
    end)
    mouse.Button1Up:connect(function()
        clicked = false
        fire.BrickColor = BrickColor.Black()
        cam.MeatLoaf.Position = _ss.Position
        laser.Transparency = 1
    end)
end)

while wait(0.05) do
    if clicked then
        fire.BrickColor = BrickColor.Red()
        local dist = (fire.CFrame.p - cam.MeatLoaf.CFrame.p).magnitude
        laser.Size = Vector3.new(0.5,0.5,dist)
        laser.CFrame = CFrame.new(fire.CFrame.p,cam.MeatLoaf.CFrame.p) * CFrame.new(0,0,-dist/2)
    end
end

Ad

Answer this question