Hello there
I need help with making a beam connect from a block to the camera. The beam does not connect to camera making it look really weird. I have a video of the problem that will explain it better.
I’ve tried to change the position of the attachments, but still dosent work.
The code is here:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local target
local down
local camPart = Instance.new("Part", workspace)
camPart.Anchored = true
camPart.CanCollide = false
camPart.Transparency = 1
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- get local player
local localPlayer = Players.LocalPlayer
-- create beam
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(255,255,255))
beam.FaceCamera = true
-- create attachments
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
-- get the mouse
local mouse = localPlayer:GetMouse()
mouse.Button1Down:connect(function()
if mouse.Target ~= nil and mouse.Target.Locked == false then
target = mouse.Target
mouse.TargetFilter = target
down = true
local gyro = Instance.new("BodyGyro")
gyro.Name = "Gyro"
gyro.Parent = target
gyro.MaxTorque = Vector3.new(500000, 500000, 500000)
local force = Instance.new("BodyPosition")
force.Name = "Force"
force.Parent = target
force.MaxForce = Vector3.new(100000, 100000, 100000)
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if down == true and target ~= nil then
camPart.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 10)
target.Gyro.CFrame = target.CFrame --this is to keep rotation
target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 20)
beam.Parent = target
attachment0.Parent = target
attachment1.Parent = camPart
-- make sure the character exists
local character = localPlayer.Character
if not character then
-- disable the beam
beam.Enabled = false
return
end
-- make sure the head exists
local head = character:FindFirstChild("Head")
if not head then
-- disable the beam
beam.Enabled = false
return
end
-- enable the beam
beam.Enabled = true
-- define origin and finish
local origin = target.Position
local finish = camPart.Position
-- move the attachments
attachment0.Position = origin
attachment1.Position = finish
end
end)
mouse.Button1Up:connect(function()
game.ReplicatedStorage.SetPartPosition:FireServer(target, target.Position)
if target then
if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then --check if there was a force
target.Gyro:Destroy()
target.Force:Destroy()
end
end
down = false
mouse.TargetFilter = nil
target = nil
beam.Enabled = false
end)
Please help me if you can!