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); }
public void Update() { // Key and mouse movement control // if (!isDead) { float horisontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(horisontal, 0f, vertical); if (movement.normalized.magnitude >= 0.1f) { Vector3 moveDestination = transform.position + movement; agent.destination = moveDestination; if (CurrentState == State.ATTACKING) { CurrentState = State.DEFAULT; currentTargetCharacterData = null; } } else { RaycastHit hit; if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50, ClickableLayer.value)) { bool enemyHit = false; if (hit.collider.gameObject.tag == "enemy") { enemyHit = true; } if (Input.GetMouseButtonDown(0)) { if (enemyHit) { Transform enemyPos = hit.collider.gameObject.transform; agent.destination = enemyPos.position; if (currentTargetCharacterData) { lastTegetCharacterData = currentTargetCharacterData; } currentTargetCharacterData = enemyPos.GetComponent <UnitData>(); } else { agent.destination = hit.point; lastTegetCharacterData = currentTargetCharacterData; currentTargetCharacterData = null; if (CurrentState == State.ATTACKING) { CurrentState = State.DEFAULT; currentTargetCharacterData = null; } } } } } if (currentTargetCharacterData && characterData.CanAttackReach(currentTargetCharacterData) && CurrentState != State.ATTACKING) { agent.Stop(); agent.ResetPath(); var targetDirection = (currentTargetCharacterData.transform.position - transform.position).normalized; targetDirection.y = 0f; transform.rotation = Quaternion.LookRotation(targetDirection); CurrentState = State.ATTACKING; } if (CurrentState == State.ATTACKING) { characterData.Attack(currentTargetCharacterData); if (!characterData.TargetIsLive(currentTargetCharacterData)) { CurrentState = State.DEFAULT; currentTargetCharacterData = null; } } } //Respawn else if (isDead) { CurrentState = State.DEFAULT; deathTimer += Time.deltaTime; if (deathTimer > 3.0f) { GoToRespawn(); } return; } if (characterData.Stats.CurrentHealth == 0) { isDead = true; _renderer.enabled = false; characterData.StartingWeapon.GetComponentInChildren <Renderer>().enabled = false; deathTimer = 0.0f; } // }