I am creating a raycast based laser gun system for fighter jets in space. Here’s what I want to do. I have an attachment for the gun itself and another attachment by default, 5000 studs away. I have a beam between both of those attachments. When the gun shoots, I fire the raycast from the gun’s worldposition to the other attachment’s worldposition. Ideally, this should intercept any parts detected. When something is hit, I move the other attachment’s worldposition to the hitposition. Then, I enable the beam for a split second. Therefore, the beam should always go 5000 studs when not hitting anything, and should hit from the gun to the hitposition. In reality, the raycast is not hitting anything and lasers go straight through.
Code:
local gunManager = {}
function gunManager.Shoot(plane)
for _, v in pairs(plane.PrimaryPart:GetChildren()) do
if v.Name == "LaserGun" then
local origin = v.WorldPosition
local destination = plane.PrimaryPart.LaserAtt.WorldPosition
local direction = (destination - origin).Unit * 5000
local RayResult = workspace:Raycast(origin, direction)
if RayResult then
plane.PrimaryPart.LaserAtt.WorldPosition = RayResult.Position
print("HIT")
else
print("Laser missed")
plane.PrimaryPart.LaserAtt.Position = Vector3.new(-0.15, -0.5, -5007)
end
plane.PrimaryPart.Beam.Enabled = true
delay(0.1, function()
plane.PrimaryPart.Beam.Enabled = false
plane.PrimaryPart.LaserAtt.Position = Vector3.new(-0.15, -0.5, -5007)
end)
end
end
end
return gunManager