Hello guys. I have the moving npc, that walks on waypoints. So, there are a bullets system, that firstly makes RayCast to send npc (by BinableEvent) ray cframe (CFrame.new(orig, dir)) and then creates a bullet part:
local orig = part.Position
local dir = (root.Position-part.Position)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace.Map, workspace.Points}
params.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(orig, dir, params)
if ray~=nil then
local hit:Instance = ray.Instance
if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
local model:Model = hit.Parent
local root:BasePart = model:FindFirstChild("HumanoidRootPart")
local cf = CFrame.new(orig, dir)
if model:FindFirstChild("RayDir") then
model.RayDir:Fire(cf)
end
end
end
local bullet = Instance.new("Part", workspace)
bullet.CanCollide = false
bullet.Anchored = true
bullet.Size = Vector3.new(.5,.5,.5)
bullet.Shape = Enum.PartType.Ball
local velo = root.Velocity
bullet.CFrame = CFrame.new(orig, root.Position+(Vector3.new(root.Velocity.X, 0, root.Velocity.Z)/(Vector2.new(orig.X, orig.Z)-Vector2.new(root.Position.X, root.Position.Z)).Magnitude*(speed*Random.new():NextNumber(1.5,2))))
game.Debris:AddItem(bullet, 2)
local touched = false
spawn(function()
while task.wait() do
if bullet==nil or touched then break end
game.TweenService:Create(bullet, TweenInfo.new(0.001), {CFrame = bullet.CFrame*CFrame.new(0,0,-speed)}):Play()
local params = OverlapParams.new()
params.FilterDescendantsInstances = {part, workspace.Points, bullet, workspace.Map}
params.FilterType = Enum.RaycastFilterType.Exclude
local parts = workspace:GetPartBoundsInBox(bullet.CFrame, bullet.Size*2, params)
for _,hit:Instance in pairs(parts) do
if not touched then
if hit.Parent:IsA("Model") and hit.Parent:FindFirstChildOfClass("Humanoid") then
touched = true
local model:Model = hit.Parent
print('touched')
bullet:Destroy()
end
break
end
end
end
end)
I have a while
cycle to move the npc, there are check, like, if dodge_bullet then (npc needa dodge the bullet) end
in cycle. Idk how to realize npc dodging bullets.
NPC AI:
local npc = script.Parent
local hum = npc:WaitForChild("Humanoid", math.huge)
local root = npc:WaitForChild("HumanoidRootPart", math.huge)
local points = workspace.Points
function GetPoint(): BasePart
return points:GetChildren()[math.random(1, #points:GetChildren())]
end
local move_db = false
local move_from_bullet = false
local ray_cf = CFrame.new()
local move_to_point
local last_point = nil
npc.RayDir.Event:Connect(function(cf)
move_from_bullet = true
ray_cf = cf
end)
while task.wait() do
if move_from_bullet then
if move_to_point~=nil then task.cancel(move_to_point) end
move_to_point = nil
move_db = false
local path = game.PathfindingService:CreatePath()
local dot = ray_cf.LookVector:Dot(root.CFrame.LookVector)
local move_dir = Vector3.new()
local LV = root.CFrame.LookVector
local RV = root.CFrame.RightVector
--//DODGE BULLET
path:ComputeAsync(root.Position, root.Position+(move_dir*10))
local waypoints = path:GetWaypoints()
for _, waypoint:PathWaypoint in pairs(waypoints) do
hum:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
hum.MoveToFinished:Wait()
end
move_from_bullet = false
else
if not move_db then
move_db = true
move_to_point = task.spawn(function()
local point = (last_point==nil and GetPoint() or last_point)
local path = game.PathfindingService:CreatePath()
path:ComputeAsync(root.Position, point.Position)
local waypoints = path:GetWaypoints()
last_point = point
for _, waypoint:PathWaypoint in pairs(waypoints) do
hum:MoveTo(waypoint.Position)
while wait() do
if hum.WalkToPoint.Y>root.Position.Y+1.5 then
hum.Jump = true
break
end
if (hum.WalkToPoint-root.Position).Magnitude<1 then
break
end
end
end
last_point=nil
task.delay(2, function() move_db = false end)
end)
end
end
end