0
0
Unityframework~10 mins

State machines for AI behavior in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an enum for AI states.

Unity
public enum AIState { Idle, [1], Attack }
Drag options to blanks, or click blank then click option'
APatrol
BRun
CJump
DSleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated states like 'Run' or 'Jump' which are not typical AI states here.
2fill in blank
medium

Complete the code to set the initial AI state to Idle.

Unity
private AIState currentState = [1];
Drag options to blanks, or click blank then click option'
AAIState.Patrol
BAIState.Attack
CAIState.Idle
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the state to Patrol or Attack initially without reason.
3fill in blank
hard

Fix the error in the switch statement to handle the AI states correctly.

Unity
switch (currentState) {
    case AIState.Idle:
        IdleBehavior();
        break;
    case AIState.Patrol:
        [1]();
        break;
    case AIState.Attack:
        AttackBehavior();
        break;
}
Drag options to blanks, or click blank then click option'
APatrolBehavior
BIdleBehavior
CAttackBehavior
DRunBehavior
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the wrong behavior method like IdleBehavior or AttackBehavior in the Patrol case.
4fill in blank
hard

Fill both blanks to update the AI state from Idle to Patrol when a condition is met.

Unity
if (currentState == [1] && seesEnemy == false) {
    currentState = [2];
}
Drag options to blanks, or click blank then click option'
AAIState.Idle
BAIState.Attack
CAIState.Patrol
DAIState.Sleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Attack or Sleep states incorrectly in this condition.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping AI states to their behavior methods.

Unity
private Dictionary<AIState, Action> stateActions = new Dictionary<AIState, Action>() {
    { [1], IdleBehavior },
    { [2], PatrolBehavior },
    { [3], AttackBehavior }
};
Drag options to blanks, or click blank then click option'
AAIState.Idle
BAIState.Patrol
CAIState.Attack
DAIState.Sleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong states like Sleep or mismatching keys and methods.