protected override void Update() { base.Update(); m_currState.StateUpdate(out m_nextState); if (IsDead) { m_nextState = GetComponent <Death>(); } else if (Scared) { m_nextState = GetComponent <Scareness>(); Scared = false; } else if (m_currState == (m_movementState as IState) || m_currState == (m_huntState as IState)) { RaycastHit2D[] hits; hits = Physics2D.RaycastAll(transform.position, EntityRight, m_detectionDistance, m_prayLayer); if (hits.Length > 0) { Transform closest = hits[0].transform; float closestDistance = Vector2.Distance(closest.position, transform.position); foreach (RaycastHit2D hit in hits) { Alien alien = hit.transform.GetComponent <Alien>(); if (alien) { alien.Scare(FacingRight); if (m_currState == (m_movementState as IState)) { float distance = Vector2.Distance(alien.transform.position, transform.position); if (distance < closestDistance) { closestDistance = distance; closest = alien.transform; } } } } if (m_currState == (m_movementState as IState)) { m_huntState.Chase(closest.transform); m_nextState = m_huntState; } } } if (m_nextState != m_currState) { SetStateActive(m_currState, false); SetStateActive(m_nextState, true); m_currState = m_nextState; } }
public void StateUpdate(out IState nextState) { m_timer -= Time.deltaTime; if (m_timer < 0f) { RaycastHit2D hit; GetComponent <Animator>().SetBool("Eat", false); if (m_animal.SearchPrey(out hit)) { Hunt hunt = GetComponent <Hunt>(); hunt.Chase(hit.transform); nextState = hunt; } else { nextState = GetComponent <Movement>(); } } else { nextState = this; } }