Be the first to post for 'AI Behaviour Tree In Unity'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

I guess this functions, but with the current code when the enemy is close to the player the ‘state’ alternates between attacking and chasing every frame. Not really clear how coroutines are contributing to the desired behaviour when they get cancelled every frame. Maybe we fix this later?

We fix so much later, certainly including this code.

Just an optimization — you don’t need character as a member variable, you can get it once in the start of the coroutine outside of the loop. Second, I think it’s better to use while (state == State.chasing) as your coroutine loop instead of checking the distance a second time when you’re already checking it once in the update loop and using it to set the state.

Privacy & Terms