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 !