0
\$\begingroup\$

I'm using a state machine for my enemy's AI. I've not worked with state machines before & followed Unity's live training on State Machines.

I added a HitState to handle animation sequences. The states are switching properly but, the animation that needs to play isn't & the NPC goes back to the attack state but is stuck playing a walk sequence.

The HitState is triggered by the following in the StatePatternEnemy:

private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("fireBall"))
        {
            ///create a hit state to move player
            ///back & play the hit animation after coroutine
            currentState = _hitState;
            Debug.Log("fire");
            NmeGotHit();
        }
    }
public void NmeGotHit()
{
   transform.Translate(0f,0.25f,-0.125f);
   AnimGotHit();
   print("CurrentState in NmeGotHit: " + currentState);
   _navMeshAgent.Stop();
   StartCoroutine(NmeHitSqnz());
}
public void AnimGotHit()
{
    GetComponent<Animation>().Play("nmeGotHit", PlayMode.StopAll);
    GetComponent<Animation>().wrapMode = WrapMode.Once;
    GetComponent<Animation>()["nmeGotHit"].speed = animGotHitSpeed;
}     
IEnumerator NmeHitSqnz()
{
   print("CurrentState in hit sqnz: " + currentState);
   yield return new WaitForSeconds(hitNmeSqnzTimer);
   _navMeshAgent.Resume();
   currentState = _attackState;
   print("CurrentState after switch in coroutine: " + currentState);
}

This is the code for HitState:

using UnityEngine;
using System.Collections;
/// <summary>
/// Attack state stays within attack.
/// Exiting out of attack sequence is handled within
/// StatePatternEnemy
/// </summary>
public class HitState : IEnemyState
{
    private float searchTimer;
    private readonly StatePatternEnemy enemy;

    public HitState (StatePatternEnemy _statePatternEnemy)
    {
        enemy = _statePatternEnemy;
    }

    public void UpdateState()
    {
        Look();
        NmeIsHit();
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("fireBall"))
        {
            Debug.Log("!");
            enemy.currentState = enemy._hitState;

        }
    }

    public void ToPatrolState()
    {
        enemy.currentState = enemy._patrolState;
        searchTimer = 0f;
    }

    public void ToAlertState()
    {
        enemy.currentState = enemy._alertState;
        searchTimer = 0f;
    }

    public void ToChaseState()
    {
        enemy.currentState = enemy._chaseState;
        searchTimer = 0f;
    }

    public void ToAttackState()
    {
        enemy.currentState = enemy._attackState;
        searchTimer = 0f;
    }
    public void ToHitState()
    {

    }
    private void Look()
    {
        RaycastHit hit;
        Vector3 enemyToTarget = ((enemy.chaseTarget.position + enemy.offset) - enemy.eyes.transform.position);
        if(Physics.Raycast(enemy.eyes.position, enemyToTarget, out hit, enemy.sightRange) && hit.collider.CompareTag("Player"))
        {
            enemy.chaseTarget = hit.transform;
        }
        else//if player is hidden then
        {
            ToAlertState();
        }
    }
    private void NmeIsHit()
    {
        Debug.Log("HitState.cs");
        Debug.Log("NmeIsHit() state: " + enemy.currentState);
        enemy.currentState = enemy._attackState;
        Debug.Log("NmeIsHit() After state switch: " + enemy.currentState);

    }
}

Thank you

\$\endgroup\$
  • 5
    \$\begingroup\$ Just a tip: use real words in your names. "enemy" instead of "nme" and "sequence" instead of "sqnz". Modern IDEs with autocomplete save you the trouble of typing all the characters, and it's easier to read. \$\endgroup\$ – Almo Sep 19 '15 at 22:58
  • \$\begingroup\$ Laziness to the extreme lol... \$\endgroup\$ – Savlon Sep 20 '15 at 10:32
  • \$\begingroup\$ Excuse me for short handing...there aren't other programmers working on this. It's why I'm asking for help. \$\endgroup\$ – Mark Laurel Sep 20 '15 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.