I’m making a game where players play as rock, paper, or scissors. In this game there’s bots, and they run on a very simple AI that looks like this:
local Goal
local ClosestEnemy, EDist = Closest(Char.Pos, Enemies)
local ClosestPrey, PDist = Closest(Char.Pos, Prey)
if EDist and PDist then
if EDist < PDist then -- RUN!!!
local Angle = math.atan2(ClosestEnemy.Pos.Y - Char.Pos.Y, ClosestEnemy.Pos.X - Char.Pos.X) + math.pi
Goal = Vector2.new(
Char.Pos.X + (Speed * math.cos(Angle)),
Char.Pos.Y + (Speed * math.sin(Angle))
)
else -- KILL!!!
local Angle = math.atan2(ClosestPrey.Pos.Y - Char.Pos.Y, ClosestPrey.Pos.X - Char.Pos.X)
Goal = Vector2.new(
Char.Pos.X + (Speed * math.cos(Angle)),
Char.Pos.Y + (Speed * math.sin(Angle))
)
end
elseif EDist then -- RUN!!!
local Angle = math.atan2(ClosestEnemy.Pos.Y - Char.Pos.Y, ClosestEnemy.Pos.X - Char.Pos.X) + math.pi
Goal = Vector2.new(
Char.Pos.X + (Speed * math.cos(Angle)),
Char.Pos.Y + (Speed * math.sin(Angle))
)
elseif PDist then -- KILL!!!
local Angle = math.atan2(ClosestPrey.Pos.Y - Char.Pos.Y, ClosestPrey.Pos.X - Char.Pos.X)
Goal = Vector2.new(
Char.Pos.X + (Speed * math.cos(Angle)),
Char.Pos.Y + (Speed * math.sin(Angle))
)
end
if Goal then Char.Pos = Goal end
Char.Pos = Vector2.new(math.clamp(Char.Pos.X, 20, 1900), math.clamp(Char.Pos.Y, 20, 1060))
Nothing here is particularly important, it gets all of its enemies and its prey and whichever is closest go toward it. This part is what I need help with:
Char.Pos = Vector2.new(math.clamp(Char.Pos.X, 20, 1900), math.clamp(Char.Pos.Y, 20, 1060))
This basically locks the bots onto the game area if their MoveTo, essentially, is beyond the wall they just run into the wall.
What I want is for them to figure out which way is ideal to run if their goal is beyond the border.
This is their behavior currently.

This is what I want them to do.

I honestly don’t even know where to start, any ideas?