Hello everyone, I’m currently developing an FPS game and need a bullet tracer like Phantom Force. I’m using a module script like Fastcast written by AI, and the bullet object uses Trail.
However, it doesn’t feel like a bullet tracer like Phantom Force. Could you help me?
youtube
Youtube
I took the picture with my cell phone because my laptop specs aren’t that great.
Image
GPT_Cast
Script
-- by: loam5442 and ChatGPT (OpenAI)
local RunService = game:GetService("RunService")
local Cast = {}
Cast.__index = Cast
local function newSignal()
local b = Instance.new("BindableEvent")
return {
Connect = function(_, f) return b.Event:Connect(f) end,
Fire = function(_, ...) b:Fire(...) end,
}
end
function Cast.new()
local self = setmetatable({}, Cast)
self.Velocity = 500
self.Acceleration = Vector3.new(0, 0, 0)
self.BounceEnabled = false
self.BounceNumber = 0
self.InfiniteBounce = false
self.Friction = 0.2
self.Elastic = 1
self.BulletObject = nil
self.BulletTime = 10
self.TimeoutOnRayhitEventEnabled = false
self.MaxDistance = 2000
self.Drag = 0
self.GravityMultiplier = 1
self.DropOff = 0.05
self.Visualize = false
self.IgnoreWater = true
self.BulletSize = 0.2
self.BulletColor = Color3.new(1,0,0)
self.CastBehavior = Enum.RaycastFilterType.Blacklist
self.DestroyOnHit = true
self.AlignToVelocity = true
self.Spread = 0
self._active = {}
self._maid = {}
self.OnRayHit = newSignal()
self.Bounced = newSignal()
self.OnTerminated = newSignal()
return self
end
local function applySpread(direction, spreadAngle)
if spreadAngle <= 0 then
--return direction.Unit
end
local spreadRad = math.rad(spreadAngle)
local angle = math.random() * spreadRad
local theta = math.random() * 2 * math.pi
local right = direction:Cross(Vector3.new(0,1,0))
if right.Magnitude == 0 then
right = direction:Cross(Vector3.new(1,0,0))
end
right = right.Unit
local up = right:Cross(direction).Unit
local offset = (right * math.cos(theta) + up * math.sin(theta)) * math.tan(angle)
local finalDir = (direction + offset).Unit
return finalDir
end
function Cast:Fire(origin:Vector3, direction:Vector3, params:RaycastParams, cl:boolean)
local dir = applySpread(direction, self.Spread)
local bullet = {
Position = origin,
Velocity = dir * self.Velocity,
Distance = 0,
Bounces = 0,
Alive = true,
StartTime = tick(),
}
if self.BulletObject then
local obj = self.BulletObject:Clone()
obj.CFrame = CFrame.new(origin, origin + direction)
obj.Parent = workspace.Bullets
bullet.Object = obj
bullet.Object.CFrame = CFrame.new(origin, origin + direction)
end
table.insert(self._active, bullet)
if not self._maid.step then
if cl then
self._maid.step = RunService.RenderStepped:Connect(function(dt)
self:_step(dt, params)
end)
else
self._maid.step = RunService.Heartbeat:Connect(function(dt)
self:_step(dt, params)
end)
end
end
end
function Cast:_step(dt, params)
local alive = 0
for _, bullet in ipairs(self._active) do
if bullet.Alive then
alive += 1
bullet.Velocity += (self.Acceleration * self.GravityMultiplier) * dt
bullet.Velocity *= (1 - (self.Drag * dt))
local move = bullet.Velocity * dt
local result = workspace:Raycast(bullet.Position, move, params)
if bullet.Object then
if self.AlignToVelocity then
bullet.Object.CFrame = CFrame.new(bullet.Position, bullet.Position + move)
else
bullet.Object.CFrame = CFrame.new(bullet.Position)
end
end
if result then
if bullet.Object then
if self.AlignToVelocity then
bullet.Object.CFrame = CFrame.new(result.Position)
else
bullet.Object.CFrame = CFrame.new(bullet.Position)
end
end
local dropoff = 1 - (bullet.Distance / self.MaxDistance)
self.OnRayHit:Fire(result, bullet, dropoff)
if self.BounceEnabled and (self.InfiniteBounce or bullet.Bounces < self.BounceNumber) then
local n = result.Normal
bullet.Velocity = (bullet.Velocity - 2 * bullet.Velocity:Dot(n) * n) * self.Elastic
bullet.Velocity *= (1 - self.Friction)
bullet.Position = result.Position + n * 0.01
bullet.Bounces += 1
self.Bounced:Fire(result, bullet)
else
if self.DestroyOnHit then
bullet.Alive = false
if bullet.Object then bullet.Object:Destroy() end
self.OnTerminated:Fire(bullet)
end
end
else
if bullet.Object then
if self.AlignToVelocity then
bullet.Object.CFrame = CFrame.new(bullet.Position, bullet.Position + move)
else
bullet.Object.CFrame = CFrame.new(bullet.Position)
end
end
bullet.Position += move
bullet.Distance += move.Magnitude
if self.Visualize then
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Color = self.BulletColor
part.Size = Vector3.new(self.BulletSize, self.BulletSize, move.Magnitude)
part.CFrame = CFrame.new(bullet.Position + move/2, bullet.Position + move)
part.Material = Enum.Material.Neon
part.Parent = workspace
game:GetService("Debris"):AddItem(part, 0.1)
end
if bullet.Distance >= self.MaxDistance or tick() - bullet.StartTime >= self.BulletTime then
bullet.Alive = false
if bullet.Object then bullet.Object:Destroy() end
self.OnTerminated:Fire(bullet)
end
end
end
end
if alive == 0 and self._maid.step then
self._maid.step:Disconnect()
self._maid.step = nil
end
end
return Cast
