private void HandleAttack() { int mouse = 0; if (Input.GetKeyDown(KeyCode.Mouse0)) { mouse = 1; } else if (Input.GetKeyDown(KeyCode.Mouse1)) { mouse = 2; } if (mouse != 0 && !isBusy) { // Debug.Log("攻击"); Vector3 vector3 = Input.mousePosition; spriteAnimator.Attack(mouse, vector3); isBusy = true; } }
private void Update() { // If Crystal was destroyed, have all enemies stop all movement. if (GameController.GameOver()) { if (navMeshAgent.velocity.magnitude > 0.0f) { DelayReaction(); } return; } // If no target detected, move towards the Crystal. if (attackTarget == null) { if (movementTimer > movementDelay) { navMeshAgent.SetDestination(CrystalController.GetInstance().transform.position); movementTimer = 0.0f; } } // Target is detected. else { // If close enough to attack target. if (Vector3.Distance(attackTarget.transform.position, transform.position) < attackRange) { navMeshAgent.velocity = Vector3.zero; navMeshAgent.ResetPath(); // Start attacking after delay has been reached. if (attackTimer > attackDelay) { // Align sprite to always face target. spriteDirection = Vector3.ProjectOnPlane(attackTarget.transform.position - transform.position, Camera.main.transform.forward).normalized; if (spriteAnimator != null) { spriteAnimator.Attack(); } // Player sound and remove health from target. AudioController.Attack(); StartCoroutine(DelayedAttack(0.2f)); attackTimer = 0.0f; } movementTimer = 0.0f; attackTimer += Time.deltaTime; } // Not close enough to attack, keep chasing target. else if (movementTimer > movementDelay) { navMeshAgent.SetDestination(attackTarget.transform.position); movementTimer = 0.0f; } } movementTimer += Time.deltaTime; }