Problem with my AI!

Hi !
I’m already trying to recreate clash royal on roblox, so I have made my own AI but I’ve got a little issue, when they are fighting each other, they do something really ugly, they pile up !
I want to know how can I prevent that because I’ve think about place some part around each AI but I think there’s a math solution !
Here the code :

local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local WalkSpeed = Humanoid.WalkSpeed
local EnemyID = tostring((tonumber(Character.Name)-1)^2)
local Left = workspace:WaitForChild("Left"..EnemyID)
local Right = workspace:WaitForChild("Right"..EnemyID)
local LeftPosition = Left.Position
local RightPosition = Right.Position
local EnemyTeam = workspace:WaitForChild("T"..EnemyID)
local EnemyMagnitude = {}
local Path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false
})

Character.Parent = workspace:WaitForChild("T"..Character.Name)

Humanoid.Died:Connect(function()
	Character:Destroy()
end)

while wait() do
	if 0 < #EnemyTeam:GetChildren() then
		for v, Enemy in pairs(EnemyTeam:GetChildren()) do
			local CharacterPosition = Character.PrimaryPart.Position
			local EnemyPosition = Enemy.PrimaryPart.Position
			local Magnitude = (CharacterPosition - EnemyPosition).Magnitude
			EnemyMagnitude[Enemy] = Magnitude
			local SmallestValue = math.huge
			for Key, Value in pairs(EnemyMagnitude) do
				if Value < SmallestValue then
					SmallestKey, SmallestValue = Key, Value
				end
			end
			if SmallestValue <= 20 then
				local TargetPosition = SmallestKey.PrimaryPart.Position
				local TargetWalkSpeed = SmallestKey.Humanoid.WalkSpeed
				if WalkSpeed > TargetWalkSpeed then
					Path:ComputeAsync(CharacterPosition, CharacterPosition:lerp(TargetPosition, (WalkSpeed - TargetWalkSpeed)/WalkSpeed))
					local PathWaypoints = Path:GetWaypoints()
					Humanoid:MoveTo(PathWaypoints[2].Position)
				elseif WalkSpeed < TargetWalkSpeed then
					Path:ComputeAsync(CharacterPosition, CharacterPosition:lerp(TargetPosition, WalkSpeed/TargetWalkSpeed))
					local PathWaypoints = Path:GetWaypoints()
					Humanoid:MoveTo(PathWaypoints[2].Position)
				else
					Path:ComputeAsync(CharacterPosition, CharacterPosition:lerp(TargetPosition, 0.5))
					local PathWaypoints = Path:GetWaypoints()
					Humanoid:MoveTo(PathWaypoints[2].Position)
				end
			else
				local CharacterPosition = Character.PrimaryPart.Position
				local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
				local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
				if LeftMagnitude < RightMagnitude then
					Path:ComputeAsync(CharacterPosition, LeftPosition)
					local PathWaypoints = Path:GetWaypoints()
					Humanoid:MoveTo(PathWaypoints[2].Position)
				elseif LeftMagnitude > RightMagnitude then
					Path:ComputeAsync(CharacterPosition, RightPosition)
					local PathWaypoints = Path:GetWaypoints()
					Humanoid:MoveTo(PathWaypoints[2].Position)
				end
			end
		end
	else
		local CharacterPosition = Character.PrimaryPart.Position
		local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
		local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
		if LeftMagnitude < RightMagnitude then
			Path:ComputeAsync(CharacterPosition, LeftPosition)
			local PathWaypoints = Path:GetWaypoints()
			Humanoid:MoveTo(PathWaypoints[2].Position)
		elseif LeftMagnitude > RightMagnitude then
			Path:ComputeAsync(CharacterPosition, RightPosition)
			local PathWaypoints = Path:GetWaypoints()
			Humanoid:MoveTo(PathWaypoints[2].Position)
		end
	end
end

Here a picture of the issue :


Thanks you for the reply and have a nice day !

1 Like

Implement one of the flocking algorithm’s element. (that allows to avoid “Steering away”)

2 Likes

I’m lost, how can I implement that in my AI system ?
Thanks you for the reply and have a nice day !

 local waypointInfluenceFactor:number = 50 --it tells you how bad it want's to go to the waypoint
 local neighboursInfluenceFactor:number = 5 --It's as same as the previous one but instead the other 
 local influenceDistance = 10--only for the bots

func ...(bots,botsAmount)
 local influence =  relativePositionToWaypoint.Unit*waypointInfluenceFactor--it's a relative position to which your AI bot want's to go toway
 for _,bot in bots do 
   if enemy(bot) then --We don't want to AI bots to run away from each other
    continue 
   end
   local RelativePosition = (MyPosition - bot.Position)
   local DistanceFrom = RelativePosition.Magnitude
   local InfluenceScaled = DistanceFrom/Distance * RelativePosition.Unit * neighboursInfluenceFactor
   Influence -= InfluenceScaled/BotsAmount
 end
return influence
end
3 Likes

Ok, I have understand but it’s not what I want to do, I want to my AI fight each other but dont pile up, basically they fight but if there is no place around the target go fight another target !

1 Like