O'Reilly logo

Unity AI Programming Essentials by Dan Violet Sagmiller, Curtis Bennett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating cover AI

Our AI enemy will just keep attacking the player as long as it is close enough to the ship. However, this isn't very realistic; we'd like the enemy ship to attack for a little bit but then duck and head for cover. We could have this hiding behavior be based on a response to the player fighting back, but for this demo, we will make it a constant value of 5 seconds; after attacking the player for 5 seconds, it will hide.

To set this up, first we'll add an isHidingbool variable to our behavior tree that is set to true after 5 seconds of attacking. Create a new constraint node under the root parallel node with the playerAttack != null && isHiding == false expression. This node's children start when playerAttack is valid and we are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required