Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local beamEvent = Instance.new("RemoteEvent")
- beamEvent.Name = "BeamEvent"
- beamEvent.Parent = game.ReplicatedStorage
- local function MoveTowards(current, target, maxDistanceDelta)
- local a = target - current
- local magnitude = a.magnitude
- if magnitude <= maxDistanceDelta or magnitude == 0 then
- return target
- end
- return current + a / magnitude * maxDistanceDelta
- end
- local function PositionBeam(p1, p2, beam, beamWidth)
- local x = CFrame.new((p1 + p2) / 2, p2)
- local mag = ( p1 - p2 ).magnitude
- beam.Size = Vector3.new(beamWidth, beamWidth, mag)
- beam.CFrame = x
- end
- beamEvent.OnServerEvent:connect(function(player, mouseHit, wandEndPoint)
- -- Change these to customize the beam movement
- local moveSpeed = 8
- local maxDisplacement = 2
- local beamTravelDistance = 200
- local beamWidth = 0.3
- local offsetInterval = 1
- local beam = Instance.new("Part", workspace)
- beam.BrickColor = script.Color.Value
- beam.FormFactor = "Custom"
- beam.Size = Vector3.new(beamWidth, beamWidth, 4)
- beam.Material = "Neon"
- beam.Transparency = 0.11
- beam.Anchored = true
- beam.Locked = true
- beam.CanCollide = false
- beam.Shape = Enum.PartType.Block
- local tip = CFrame.new(wandEndPoint)
- local rotated = tip * CFrame.fromAxisAngle(player.Character.Head.CFrame:toAxisAngle())
- local worldSpace = rotated:toWorldSpace(CFrame.new(0, 0, -beam.Size.Z / 2))
- beam.CFrame = worldSpace
- beam.Touched:connect(function(otherPart)
- -- Beam touched something - make sure it wasn't us
- if player.Character and not otherPart:IsDescendantOf(player.Character) then
- -- Check to see if it was a character
- local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
- if not humanoid then
- humanoid = otherPart.Parent.Parent:FindFirstChild("Humanoid")
- end
- if humanoid then
- -- We hit somebody - add hit code here
- humanoid:TakeDamage(110)
- end
- beam:Destroy()
- end
- end)
- local destination = wandEndPoint + (mouseHit - wandEndPoint).unit * beamTravelDistance
- -- Need to manually check for collisions with terrain in between our gun barrel and target point. Terrain dosn't fire Touched event
- local ray = Ray.new(wandEndPoint, (mouseHit - wandEndPoint).unit * beamTravelDistance)
- local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
- if part and part.Name == "Terrain" then
- if (position - wandEndPoint).magnitude < (destination - wandEndPoint).magnitude then
- -- Hit terrain. Set beam to only travel until it hits blocking terrain
- destination = wandEndPoint + (mouseHit - wandEndPoint).unit * (position - wandEndPoint).magnitude
- end
- end
- local isMoving = true
- local rng = Random.new()
- local beamPos = beam.CFrame.p
- local count = 0
- local firstFire = true
- local previousPosition
- while isMoving do
- local newPos = MoveTowards(beamPos, destination, moveSpeed)
- if newPos:isClose(destination, 1e-7) then
- isMoving = false
- break
- end
- local center = CFrame.new(newPos, destination)
- beamPos = center.p
- if count % offsetInterval == 0 then
- if firstFire then
- firstFire = false
- beam.CFrame = center
- previousPosition = center * CFrame.new(0, 0, -beam.Size.Z/2)
- else
- local nextBeamPosition = center * CFrame.new(rng:NextNumber(-maxDisplacement, maxDisplacement), rng:NextNumber(-maxDisplacement, maxDisplacement), 0)
- PositionBeam(previousPosition.p, nextBeamPosition.p, beam, beamWidth)
- previousPosition = nextBeamPosition
- end
- end
- count = count + 1
- if count == 100 then count = 0 end
- wait()
- end
- beam:Destroy()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement