Hello there, Roblox Community. I am working on a Shooting game that will have AI NPC’s.
-
I am trying to make bullet spread using fastcast
-
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.
-
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