Trails for bullets using FastCast module

@dthecoolest made this but he didn’t include the trail object part. Does anyone know how to do it? i tried and the trail isn’t showing up for some reason

1 Like

This is what I have done for the bullet’s properties

local function getBulletWithTrailTemplate()
	local bullet = BULLET_TEMPLATE:Clone()
	local trail = BULLET_TRAIL.InstanceTemplate:Clone()
	local attachment0 = BULLET_TRAIL.Attachment0:Clone()
	local attachment1 = BULLET_TRAIL.Attachment1:Clone()

	bullet.Position = Vector3.zero
	bullet.CanCollide = false
	
	attachment0.Name = "TrailAttachment0"
	attachment1.Name = "TrailAttachment1"
	--hi
	attachment0.Parent = bullet
	attachment1.Parent = bullet
	--I'm guessing this is where the problem is?
	attachment0.Position = Vector3.new(0, 0.2, 0)
	attachment1.Position = Vector3.new(0, -0.2, 0)
	
	trail.Attachment0 = attachment0
	trail.Attachment1 = attachment1
	trail.Parent = bullet
	
	return bullet
end

local BulletCache = PartCache.new(getBulletWithTrailTemplate(), 100, BULLETS_FOLDER)

Turns out I accidentally used the normal template for PartCache.new() instead of using the function. The trail is working now but it is not visible during the first couple of segments for some reason.

The first shot worked, but the second one didn’t

1 Like

I’m going to try this now

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FastcastRedux = require(ReplicatedStorage:WaitForChild("FastCastRedux"))
local BulletTemplate = ReplicatedStorage:WaitForChild("Bullet")

local mainCastParams = RaycastParams.new()
mainCastParams.FilterType = Enum.RaycastFilterType.Exclude

local caster = FastcastRedux.new()
FastcastRedux.VisualizeCasts = false

local casterBehavior = FastcastRedux.newBehavior()
casterBehavior.RaycastParams = mainCastParams
casterBehavior.AutoIgnoreContainer = false

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local mouse = LocalPlayer:GetMouse()

local function Cast()
	local origin = script.Parent.Handle.Attachment.WorldPosition
	local direction = (mouse.Hit.Position - origin).Unit * 1000
	
	mainCastParams.FilterDescendantsInstances = { LocalPlayer.Character, workspace.Bullets }
	casterBehavior.Acceleration = Vector3.new(0, -10, 0)
	casterBehavior.MaxDistance = 1000
	casterBehavior.CosmeticBulletTemplate = BulletTemplate
	casterBehavior.CosmeticBulletContainer = workspace.Bullets
	
	local activeCast = caster:Fire(origin, direction, 1000, casterBehavior)
end

local function OnLengthChanged(ActiveCast, LastPoint, Direction, Length, Velocity, Bullet)
	
	local bulletSize = Bullet.Size.Z / 2
	local targetCFrame
	if not ActiveCast.UserData.Frames then
		ActiveCast.UserData.Frames = 0
		targetCFrame = CFrame.lookAt(LastPoint, LastPoint + Direction)
	else
		targetCFrame = CFrame.lookAt(LastPoint, LastPoint + Direction)
	end
	
	ActiveCast.UserData.Frames += 1
	Bullet.CFrame = targetCFrame

end

local function OnCastTerminating(ActiveCast)
	print(ActiveCast.UserData.Frames)
	local bulletInstance = ActiveCast.RayInfo.CosmeticBulletObject
	if bulletInstance then
		Debris:AddItem(bulletInstance, 2)
	end
end

local function OnRayHit(ActiveCast, Result, Velocity, Bullet)
	if ActiveCast.UserData.Frames <= 2 then
		local direction = Result.Position - Bullet.Position
		local originalPosition = Bullet.Position
		
		-- Sets the target position x0.5
		task.wait()
		Bullet.CFrame = Bullet.CFrame + direction * 0.5
		
		-- Sets the target position x1
		task.wait()
		Bullet.CFrame = Bullet.CFrame + direction * 0.5
	end
end

caster.LengthChanged:Connect(OnLengthChanged)
caster.CastTerminating:Connect(OnCastTerminating)
caster.RayHit:Connect(OnRayHit)
script.Parent.Activated:Connect(Cast)
2 Likes