Basic enemy AI

is was thinking maybe it could be a if else kinda thing

Where do you think you can apply that?

The ray detection

Maybe you can give it a try in de function below.

image

On line 409 you can try to add something like && !entity.tags.has('Climbable')

With this the AI will only move on the object if it coincidentally is moving that way.
Also the sensor height must be higher than the object, otherwise the AI ​​will move through it instead of on it.

All in all, I don’t think you will achieve the results you want.

If the sensor height is higher than the object, the object is already considered as climbable anyway, so the entire suggestion above is useless. Sorry.

so all I gotta do is raise the sensors?

The AI ​​will move on all objects lower than the value of the sensor height attribute. If the object is higher, it will be detected as an obstacle and the AI ​​will try to avoid it.

Suggestion, to make the enemy more customization and match the creators game better for navigation, I would suggest allowing the player to change the sensor length on the side and the front, making them separate, for example what if you want your enemy to avoid obstacles on its left and right but less in the front. This feature will make the enemies ai more advanced but also keeping it simple and easy.

What would be the use case for the AI ​​to avoid obstacles in front less?

The sensor lengths are precisely adjusted so that the balance for avoiding obstacles is optimal. If there is an imbalance, the AI ​​is more likely to move through obstacles. That’s why I think the sensors should only be extended as a whole and you basically only use this if the scaling of your character is different.

EXACLTY, to match the enemy size more correctly! Although in most cases it will be better balanced just having the option adds more customization and match the enemy scale better.

1 Like

But I think it would also be used for certain terrain types, for example, open areas will use balanced sensors, but something an area that is really space tight could be more turn sensitive.

The problem is that the script also has to be stable with every customizable part and that is not easy to achieve. I also don’t want the script to become more complex than it already is. If you study the script, you are still be able to change the raycasts.

How do I get an enemy to get through a pushable door with a collision?

Since the AI has no pathfinding and movement is based on target direction, it’s unlikely you get the AI through a small doorway anyway.

1 Like

I did it in Prasinophobia → PlayCanvas | HTML5 Game Engine
But that was pretty outdated code, I think it used tags or somthing, I just need the enemy to ignore the door like it is not there and it can get through.

I will see if I can add a way to ignore entities in the next update.

Just any entities with a “ignore tag” maybe? but also can you maybe just tell me what code to add since I already added ai to the project I am working on

Yes, it will be something like that. I can’t tell you what to do exactly as I first need to check and test. I expect you can do something similar as I describe in the post below.

Version 1.3 is released! :partying_face:

In this release a lot is changed and maybe it need to be fine tuned based on your feedback. Special thanks to @Brent_Reddick for his feedback to be able to make the right decisions and also for sharing his project to test the AI.

  • Attributes
    The available attributes are easy to use and should allow the AI ​​to work in most games.

    • View Distance
      The distance of a target is being visible.
      A view distance of zero means the enemy can’t see targets.
    • Hear Distance
      The distance of a target with Audible tag is being audible.
      A hear distance of zero means the enemy can’t hear targets.
    • Patrol Range
      The range of patrolling based on the origin.
      A patrol range of zero means the enemy will not patrol arround.
    • Combat Range
      The range of combat based on the equipment.
      A combat range of zero means the enemy will not attack.
    • Enemy Speed
      The default speed for moving and rotation.
      The actual speed of the enemy is controlled in the states.
    • Sensor Length
      The length of sensors to detect obstacles.
      The sensor length should depend on the scale and environment.
    • Sensor Height
      The height of sensors to detect obstacles.
      The sensor height should depend on the scale and environment.
    • Show sensors
      Ability to show the sensors for debugging.
  • Update Target
    This functions find the closest target for the AI based on the targets in game. You can add a new target at runtime using this.enemyTargets.push(target) or remove a target using this.enemyTargets.delete(target).

  • Update Movement
    This function moves and rotates the enemy in the direction of the destination. It avoid obstacles using the sensors. (Sinds there is no pathfinding, there are some limitations why not always the desired result can be achieved).

  • Update Animation
    This function animates your charachter based on a couple of conditions. You only have to assign the animations to the anim component and be sure it is enabled. By default no animations are used.

  • Update State
    This function only execute the current state. All other state related code is now incorporated in the specific states.

  • Enemy States
    Currently the AI has three states (patrol, chase and combat). Every state should have the information about what the AI has to do in the current state and when the AI has to change to another state.

  • Other functions
    There are a couple of other functions at the bottom of the script. One of them is the attack function, that create and fire a new bullet, when the enemy is in combat. You are free to study and change the functions.

Please note that the functions are all capitalized now, to make the script easier to read. The bullet entity is removed from the character and a template entity is used instead. The UI of the characters is removed, because it turned out that this was not used in users’ projects. I added the abiltity to ignore obstacles with an Ignore tag.

https://playcanvas.com/project/870482/overview/basic-enemy-ai

3 Likes

Cool!