SOLVED : AI enemy, how i do! - "DistanceBetweenPositions"

I’m changing the AI ​​of my game’s enemies, switching to raycast and stuff. question is:
How can I make an enemy vision without creating objects of vision? Can raycast do that?

Example:
Sem%20t%C3%ADtulo

To understand. The image shows an enemy (square) with two views, if the player is between those two views, the enemy will shoot or follow!

ps: I tried to do it with raycast only, but in this case it just does the action when it passes through the intercept point.

Hi, just a question for fully understanding the problem: What speaks against using an object of vision (e.g. a triangle)?

Think of several enemies on the screen, and several enemies with the same objects created. This can result in loss of performance.

Now when the enemy’s vision is based only on his point of view without any interference from external objects, (in the case of a triangle) in addition to being much better, it is better used on all enemies. because each enemy can be a specific size.

The idea is that the vision of all enemies is between the two imaginary lines that we can create with raycast.

Thus, the enemy has a view of only a certain part of the scenario, which would be in front of him, not above and not below.

With the number of raycasts you’re going to be doing (as raycasts are by definition rays(single lines), not cones/shapes/etc), there’s not going to be a huge difference in performance vs a collision object.

However, you can see how some examples do this by looking at the various Pathfinding examples. Many of them use raycasting.

Edit: If you do want to go down the raycasting path, you’ll likely need to combine a raycast (to check for obstacles) plus the Object.AngleToObject() expression (to ensure it’s within the “vision cone angles”). You’ll likely need to do a bit of math, and as far as I know they’ve not been used in combination that way before since the expression is relatively new.

1 Like

I second abandoning raycasts, and instead would recommend something like this:
Conditions:
For Each Enemy
{
Enemy.Variable(Target.X) == -1
Enemy.DistanceTo(Player) < Enemy.Variable(SightDistance)
abs(Enemy.AngleTo(Player) - Enemy.Angle()) < Enemy.Variable(ViewConeAngle)
Action:
Enemy.Variable(Target.X) = Player.X()
Enemy.Variable(Target.Y) = Player.Y()
Enemy.MoveTo(Target.X, Target.Y)

Condtion:
Enemy.Variable(Target.X) >= 0
Enemy.DistanceTo(Player) < Enemy.Variable(AttackDistance)
Actions:
… ATTack player…

Condtion:
Enemy.Variable(Target.X) >= 0
Enemy.DistanceTo(Player) < Enemy.Variable(GiveUpDistance)
Actions:
Enemy.Variable(Target.X) = -1
Enemy.Variable(Target.Y) = -1
}

That’s just a mock up of a basic move and investigate enemy based on view cone.

1 Like

Hello, I solved my problem using only the command “DistanceBetweenPositions”