Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RunService = game:GetService("RunService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Signal = require(ReplicatedStorage.Libs.Signal)
- local Trove = require(ReplicatedStorage.Libs.Trove)
- local IS_CLIENT = RunService:IsClient()
- local DEFAULT_SPEED = 50
- local DEFAULT_PARAMS = RaycastParams.new()
- local Bullet = {}
- Bullet.__index = Bullet
- function Bullet.new(projectileProperties)
- local self = setmetatable({}, Bullet)
- self.Properties = projectileProperties
- self.Position = projectileProperties.StartPosition
- self.Params = projectileProperties.Params or DEFAULT_PARAMS
- self.Speed = projectileProperties.Speed or DEFAULT_SPEED
- if IS_CLIENT then
- self.Instance = script.BulletTemplate:Clone()
- self.Instance.Name = "sigma"
- self.Instance.Position = self.Position
- self.Instance.Parent = workspace
- end
- self.Hit = Signal.new()
- self.Destroyed = Signal.new()
- self.Destroyed:Connect(function()
- if self.Instance then
- self.Instance:Destroy()
- end
- end)
- local distance =
- (projectileProperties.StartPosition
- - projectileProperties.TargetPosition).Magnitude
- self.Lifetime = distance / self.Speed
- return self
- end
- function Bullet:Update(_deltaTime)
- local timePassed = workspace:GetServerTimeNow() - self.Properties.StartTime
- local timePercentage = timePassed / self.Lifetime
- if timePercentage > 1 then
- self.Destroyed:Fire()
- return
- end
- local lastPosition = self.Position
- local currentPosition = self.Properties.StartPosition:Lerp(self.Properties.TargetPosition, timePercentage)
- local direction = currentPosition - lastPosition
- local result = workspace:Raycast(
- lastPosition,
- direction,
- self.Params
- )
- if result then
- self.Position = result.Position
- self.Hit:Fire(result.Instance)
- self.Destroyed:Fire()
- return
- end
- self.Position = currentPosition
- if self.Instance then
- self.Instance.Position = self.Position
- end
- end
- return Bullet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement