How to add bullet spread in FastCast-like system?

Hello there, Roblox Community. I am working on a Shooting game that will have AI NPC’s.

  1. I am trying to make bullet spread using fastcast

  2. The issue is i have tried incorporating bullet spread into this system so that when the AI shoots at the player, its not always hitting direct shots. I want the bullets to spread a little bit per shot.

  3. I have tried using the shootDirection method, but been having results such as bullets shooting the wrong direction ,or not landing shots.

This is the code i have so far, how would i go about including bullet spread?

function pushBullet(bullet,taser)
	
	local speed = 12 --// How fast the bullet travels

	local iterations = 20 --//Cycles to repeat for

	local Spread = 0.165 -- will be effected based on our target's movement (TESTING)
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {workspace.Terrain}

	runService.Heartbeat:Wait() --Give bullet time to render from barrel

	--bullet.CFrame = glock.CFrame * CFrame.new(0,0.2,-0.5)

	bullet.CFrame = top.Barrel.WorldCFrame

	runService.Heartbeat:Wait() --Give bullet time to render from barrel
	
	core.spawn(function()
		while true do
			
			
			
			local position,nextPosition = bullet.Position,Vector3.new(0,0,speed)
			
			local result = workspace:Raycast(position, bullet.CFrame.LookVector*speed,params)
			
			if result and result.Instance.Name ~= "Bullet" then	
			
				bullet.CFrame = CFrame.new(result.Position)
				
				removeBullet(bullet)

				local human = core.getHuman(result.Instance.Parent)
				impactTaser.WorldPosition = result.Position
				if human and human.Health > 0 then
					
					--//Code for hitting humanoid etc... (Removed on purpose)

				elseif not human then
					--//Code for hitting walls etc... (Removed on purpose)
				end
				break
			else
				bullet.CFrame = bullet.CFrame * CFrame.new(-nextPosition) --//If raycast doesnt hit anything so far, move forward...
			end

			iterations -= 1
			if iterations < 0 then
				bullet:Destroy()
				break
			end
			game:GetService("RunService").Heartbeat:Wait()
		end
	end)
end

Actually, I kinda solved it. Here is the solved code for anyone who needs a reference:


function moveBullet(bullet)

	local speed = 12
	
	local iterations = 20

	local Spread = 0.15 -- will be effected based on our target's movement

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {workspace.Terrain}

	runService.Heartbeat:Wait() --Give bullet time to render from barrel
	--bullet.CFrame = glock.CFrame * CFrame.new(0,0.2,-0.5)
	bullet.CFrame = top.Barrel.WorldCFrame
	runService.Heartbeat:Wait() --Give bullet time to render from barrel

	local direction = (bullet.CFrame.LookVector*speed).unit

	local spreadDirection = direction + Vector3.new(
		math.random() * Spread - Spread / 2,
		math.random() * Spread - Spread / 2,
		math.random() * Spread - Spread / 2
	)

	core.spawn(function()
		while true do

			local currentPosition = bullet.Position

			local bulletDirection = spreadDirection*speed

			local newPos = bullet.Position + spreadDirection * speed

			local result = workspace:Raycast(currentPosition, bulletDirection,params)

			if result and result.Instance.Name ~= "Bullet" then	

				bullet.Position = result.Position

				removeBullet(bullet)
				
				local human = core.getHuman(result.Instance.Parent)
				
				if human and human.Health > 0 then
					
					--// Code

				elseif not human then
				
					 --//Code
					 
				end
				break
			else
				bullet.Position = newPos
			end

			iterations -= 1
			if iterations < 0 then
				bullet:Destroy()
				break
			end
			game:GetService("RunService").Heartbeat:Wait()
		end
	end)
end

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