void CheckAttack() { if (m_CurrentState == State.ATTACKING) { return; } if (m_CharacterData.CanAttackReach(m_CurrentTargetCharacterData)) { StopAgent(); //if the mouse button isn't pressed, we do NOT attack if (Input.GetMouseButton(0)) { Vector3 forward = (m_CurrentTargetCharacterData.transform.position - transform.position); forward.y = 0; forward.Normalize(); transform.forward = forward; if (m_CharacterData.CanAttackTarget(m_CurrentTargetCharacterData)) { m_CurrentState = State.ATTACKING; m_CharacterData.AttackTriggered(); m_Animator.SetTrigger(m_AttackParamID); } } } else { m_Agent.SetDestination(m_CurrentTargetCharacterData.transform.position); } }
// Update is called once per frame void Update() { //See the Update function of CharacterControl.cs for a comment on how we could replace //this (polling health) to a callback method. if (m_CharacterData.Stats.CurrentHealth == 0) { m_Animator.SetTrigger(m_DeathAnimHash); m_CharacterAudio.Death(transform.position); m_CharacterData.Death(); if (m_LootSpawner != null) { m_LootSpawner.SpawnLoot(); } Destroy(m_Agent); Destroy(GetComponent <Collider>()); Destroy(this); return; } Vector3 playerPosition = CharacterControl.Instance.transform.position; CharacterData playerData = CharacterControl.Instance.Data; switch (m_State) { case State.IDLE: { if (Vector3.SqrMagnitude(playerPosition - transform.position) < detectionRadius * detectionRadius) { if (SpottedAudioClip.Length != 0) { SFXManager.PlaySound(SFXManager.Use.Enemies, new SFXManager.PlayData() { Clip = SpottedAudioClip[Random.Range(0, SpottedAudioClip.Length)], Position = transform.position }); } m_PursuitTimer = 4.0f; m_State = State.PURSUING; m_Agent.isStopped = false; } } break; case State.PURSUING: { float distToPlayer = Vector3.SqrMagnitude(playerPosition - transform.position); if (distToPlayer < detectionRadius * detectionRadius) { m_PursuitTimer = 4.0f; if (m_CharacterData.CanAttackTarget(playerData)) { m_CharacterData.AttackTriggered(); m_Animator.SetTrigger(m_AttackAnimHash); m_State = State.ATTACKING; m_Agent.ResetPath(); m_Agent.velocity = Vector3.zero; m_Agent.isStopped = true; } } else { if (m_PursuitTimer > 0.0f) { m_PursuitTimer -= Time.deltaTime; if (m_PursuitTimer <= 0.0f) { m_Agent.SetDestination(m_StartingAnchor); m_State = State.IDLE; } } } if (m_PursuitTimer > 0) { m_Agent.SetDestination(playerPosition); } } break; case State.ATTACKING: { if (!m_CharacterData.CanAttackReach(playerData)) { m_State = State.PURSUING; m_Agent.isStopped = false; } else { if (m_CharacterData.CanAttackTarget(playerData)) { m_CharacterData.AttackTriggered(); m_Animator.SetTrigger(m_AttackAnimHash); } } } break; } m_Animator.SetFloat(m_SpeedAnimHash, m_Agent.velocity.magnitude / Speed); }