I’m trying to make a laser sight that reacts to its surrounding, i.e the closer you get to your target, the short the laser becomes. How do I go about this?
Also, I have an issue along with it, when I toggle the laser, this happens:
local input = game:GetService("UserInputService")
local laserHolder = script.Parent:WaitForChild("LaserPart")
local character = game.Players.LocalPlayer.Character
local toggled = false
input.InputEnded:Connect(function(userInput, proc)
if userInput.UserInputType == Enum.UserInputType.Keyboard then
if userInput.KeyCode == Enum.KeyCode.F and not toggled then
toggled = true
local lookDirection = laserHolder.CFrame.RightVector
local origin = laserHolder.Position
local ray = Ray.new(origin, lookDirection * 500) -- studs
local hitPart, hitPosition = workspace:FindPartOnRay(ray, character)
local distance = (laserHolder.Position - hitPosition).Magnitude
laserHolder.Transparency = 0.6
laserHolder.Size = Vector3.new(distance, 0.05, 0.05)
laserHolder.CFrame = CFrame.new(laserHolder.CFrame.Position, hitPosition) * CFrame.new(0, 0, -distance / 2)
if hitPart then
print("Hit part: " .. hitPart:GetFullName())
else
print("Did not hit part")
end
elseif userInput.KeyCode == Enum.KeyCode.F and toggled then
toggled = false
laserHolder.Transparency = 1
end
end
end)