Advanced AI in Unity- State Machine

Notes:

  • 0:30 What you’ll be making.
  • 1:16 What we’ll be using.
  • 1:37 Open the animator

EXAMPLE STATE 1: FOLLOW

  • 1:55 Create a bool parameter to find out if the enemy is following the player.
  • 2:02 Make a transition between the idle state and the chasing state.
  • 2:12 Trigger this transition only when the enemy is following the player.
  • 2:17 Make a transition from the follow state to the idle state that will trigger if the enemy stops following the player.
  • 2:28 Add a behavior script to the chase state.
  • 2:35 Open the behavior in your scripting editor.
  • 2:41 Remove the comments on the OnStateEnter, Update, and Exit functions.
  • 3:10 Create a private variable of type Transform to store the player’s position.
  • 3:14 In the On State Enter function set the Transform variable equal to the transform information on the object tagged “Player”.
  • 3:27 In the On State Update function, make the AI follow the player.
  • 3:36 In the parentheses of MoveTowards() feed in the animator’s position, the player’s position, and a speed value, you’re probably going to want a speed variable for this.
  • 3:49 Multiply all the parameters with Time.deltaTime.
  • 3:53 Type a value for speed in the inspector.
  • 3:56 Ensure the player has the “Player tag
  • 4:12 Add a behavior to the idle state.
  • 4:25 In On State Update set the is following bool to true if the space key is pressed.
  • 4:42 In the On State Update for the chase state behavior set the is following to false if the space key is pressed.

EXAMPLE STATE 2: PATROL

  • 5:04 Make another bool parameter to check if the AI is patrolling.
  • 5:09 Make a transition from the chase animation to the patrol animation.
  • 5:13 Make this transition trigger if the AI is patrolling.
  • 5:20 Make a transition from the patrol state to the chase state that triggers if the AI is not patrolling.
  • 5:23 Add a behavior script to the patrol state.
  • 5:31 Create a patrol path and have the AI move along it. A video on the patrolling behavior will be linked here.
  • 5:40 If the player presses space, stop the AI from patrolling.
  • 5:53 In the On State Update function in the chase behavior script, add an if statement checking if the player presses P. If they do press P, make the AI start patrolling.

PLAYING SFX WITH STATE MACHINE BEHAVIORS (TIMELAPSED BONUS)

  • 6:49 In the chase behavior script, add a private variable of type AudioSource.
  • 6:49 In On State Enter set the audio source equal to that on the animator.
  • 6:50 Below that line, play the audio source.

SPAWNING PARTICLE EFFECTS WITH STAE MACHINE BEHAVIORS (TIMELAPSED BONUS #2)

  • 7:00 In the idle behavior script, add a public variable of type game object for the particle effect.
  • 7:04 Instantiate the effect at the animator’s position and no rotation for the third argument.
  • 7:05 Drag and drop the particle system game object into the empty “effect” slot in the inspector.

7:07 Final product and final words.

Leave a Reply

Your email address will not be published. Required fields are marked *