Hey, Im trying to create a grabbing beam; where if it hits a part named "Target’ it grabs the part.
My issue: it positions the part at the near middle of the prototype beam, instead of at the end. How can I fix this?
Code:
--//Variables---
local tool = script.Parent
local origin = tool:WaitForChild("Model").Origin
local laserFolder = game.Workspace.Laser
local run = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local range = 50
mouse.TargetFilter = laserFolder
--//Loop---
run.RenderStepped:Connect(function()
local mousePosition = mouse.Hit.p
local originPosition = origin.Position
local direction = (mousePosition - originPosition).Unit * range
local result = workspace:Raycast(originPosition,direction )
local beam = game.Workspace.Laser:FindFirstChild("Beam")
local midpoint = originPosition + direction/2
beam.CFrame = CFrame.new(midpoint,originPosition)
beam.Size = Vector3.new(.5,.5, direction.Magnitude)
if result then
if result.Instance.Name == "Target" then
if not beam:FindFirstChild("WeldConstraint") then
local weld = Instance.new("WeldConstraint")
weld.Parent = beam
weld.Part0 = result.Instance
weld.Part1 = beam
end
---Im having the issue here--
result.Instance.CFrame = beam.CFrame
end
end
end)