Help With Wall Avoiding AI

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.
Current

This is what I want them to do.
Goal

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

For that you should use Pathfinding Service, which allows your characters to always calculate the most logical path to their destination.

Hey, the images seen in my post aren’t recreations, they’re demonstrations. It’s a 2d game, entirely in UI so I don’t believe I have that luxury.

You can not achieve anything like that in UI, there are no collision events in ui, nor there is raycasting, or even events for detecting when a ui element reaches a border of frame or something.

That doesn’t mean you can’t do it yourself.

1 Like

That’s not true at all, I’m using custom math to determine the collisions, I’m using the polar to cartesian equation to do rudumentry ray casting, and I can tell if the point is out of the border by if its above or below a certain threshold.

I can KNOW that the AI is doing the wrong thing, what I need help with is how do I correct their angle such that if the thing they’re running from is pushing them into the border, they either go up or down the border depending on which way is most advantageous.

1 Like