Fastcast cosmetic bullet not following ray visualization

I am using fastcast to simulate bullets with a gun. However, the cosmetic bullets I have created do not follow the ray visualization(the black lines). I am a fastcast noob so i don’t know what I am doing

fastcast code:

local weapon = char:FindFirstChildOfClass("Tool")
	local origin
	
	if not weapon then return end
	
	if (localplayer.Character.Head.CFrame.p - camera.CFrame.p).Magnitude < 1 then
		origin = camera.Viewmodel:FindFirstChildOfClass("Tool").Model.Shoot.Position
		
	else
		origin = weapon.Model.Shoot.Position
	end
	
	local tool = char:FindFirstChildOfClass("Tool")
	local itemstats = require(tool.Itemstats)
	
	local mousecastparams = RaycastParams.new()
	mousecastparams.IgnoreWater = true
	mousecastparams.FilterType = Enum.RaycastFilterType.Exclude
	mousecastparams.FilterDescendantsInstances = {camera.Viewmodel, localplayer.Character}

	local direction = (GetMouse(itemstats.weaponstats.range, mousecastparams) - origin).Unit

	local castbehavior = fastcast.newBehavior()
	castbehavior.Acceleration = replicatedstorage.bulletGravity.Value
	castbehavior.RaycastParams = params
	castbehavior.AutoIgnoreContainer = false
	castbehavior.CosmeticBulletContainer = workspace.CosmeticBulletsContainer
	castbehavior.CosmeticBulletTemplate = replicatedstorage.GameItems.Bullet
	
	caster:Fire(origin, direction, itemstats.weaponstats.velocity, castbehavior)
	
	caster.RayHit:Connect(function(hit)
		print(hit.Instance)
	end)

you can see in the video how the console prints nil for hit.instance

caster.RayHit should be connected only one time at the beginning of the script

also you need to update the cosmetic bullet yourself

edit: this one should also be connected just one time at the beginning of the script

caster.LengthChanged:Connect(function(cast, segmentOrigin, segmentDirection, length, segmentVelocity, cosmeticBulletObject)

	if cosmeticBulletObject == nil then return end

	local bulletLength = cosmeticBulletObject.Size.Z / 2 
	local baseCFrame = CFrame.new(segmentOrigin, segmentOrigin + segmentDirection)
	cosmeticBulletObject.CFrame = baseCFrame * CFrame.new(0, 0, -(length - bulletLength))

end)
1 Like

This was just for testing purposes to see what it was hitting

This is done with the lengthchanged, correct? I am mostly using a version of this script i created a while ago and can use that function instead of the one you provided

1 Like

yes it calls lengthchanged when the bullet moves

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.