Modify Enemy AI

I fixed it, I file was causing it. He still goes through stuff.

That’s what you tried to achieve?

No sorry, but he keeps going through stuff so i thought I could add something that makes him see the obstacle. If it sees something in front of the enemy’s path it would turn the enemy a different way so it does not hit it.

You asked for the opposite first or at least I misunderstood what you try to achieve.

The basic enemy AI script has this functionality already. With a correct setup the enemy avoid obstacles.

Please revert the last chance I suggested and share a link of your project so I can check your setup and behavior of the enemy.

This is what I have The camera entity is in the player because it it first person
Screenshot 2022-06-06 1.16.33 PM

Looks correct so far. Can you share your project so I can check the behavior of the enemy?

Sure!

Editor: PlayCanvas | HTML5 Game Engine

You can launch it from the editor.

ohhhh, i thought it was the project we are working on lol, it is the same script though so i was a little confused bc it has the same script and setup

I see obstacles currently need to have the name ‘Obstacle’.

image

What you can do, is remove the name check and only leave the result check.

if (result) {

When I have some time, I will modify the example project, to make it easier to integrate into other projects.

Hi! @Albertos, I have checked the enemy AI project and you have made a good amount of improvements to it. This will really help with others future projects and my projects as well.

1 Like

Thank you very much @Mason_Rocchio!

1 Like

I added a new state called near

if the player is less that a distance of 10 from the neighbor a certain sound would play letting the player know he is close by.

Enemy AI review all modes.

Mode 1, “Idle”

In Idle mode, the enemy is living its normal life. He eats, drinks, waters his plants, and more.

Mode 2 “Hunt”

In hunt mode, the enemy is searching for the player. This mode can be triggered if he notices something is off. Or if the player makes a noise.

Mode 3 “Chase”

In chase mode the enemy is chasing the player. During the chase the enemy will learn from the players moves using key spots. Whenever the player walks over the key spots. the enemy will analyse how much the player walks over that spot. The enemy will check those spots more often to see if the player is there.

Mode 4 “Attack”

In attack mode, the enemy is catching the player.

Interesting idea! I will keep this one in mind for my own game(s). Thanks for sharing! :innocent:

1 Like

Hi! @Albertos, How would I make it so if the enemy sees the target the animation switches. And if it does not see the target is switches to another animation?

Here is what I have

if(this.enemyCanSeeTarget) {
     this.entity.anim.baseLayer.activeState('chase');
     this.entity.anim.setBoolean('chase', true);
}
else{
     this.entity.anim.baseLayer.activeState('walk');
     this.entity.anim.setBoolean('chase', false);
};

I would do this in the setState function. How you have to do this, is based on the setup of your anim state graph.

so is that code correct?

The code is written correctly if you don’t get an error or warning in the code editor and launch. (As far I can see the last ; sign should not be there and I don’t think activeState is a function). The code logic is correct if the result is what you expect.