///攻击 public override void StateAttack() { if (enemyPawn == null) { distance = 0; ChangeIdle(); } else { if (AttackLock == false) { int j = Random.Range(0, 2); //攻击动画随机 AttackLock = true; //攻击锁 agent.speed = 0; gameObject.transform.LookAt(enemyPawn.transform.position); switch (j) { case 0: animator.SetTrigger("attack1"); break; case 1: animator.SetTrigger("attack2"); break; default: animator.SetTrigger("attack3"); break; } if (RectangleAttack(transform, enemyPawn, attackDistance, attackDistance)) { var hit = enemyPawn.gameObject; if (hit.GetComponent <Pawn1>() != null) { Pawn1 pawn = hit.GetComponent <Pawn1>(); if (pawn != null) { //Debug.Log("hp:-10"); pawn.TakeDamage(attack); } } else if (hit.GetComponent <Pawn2>() != null) { Pawn2 pawn = hit.GetComponent <Pawn2>(); if (pawn != null) { //Debug.Log("hp:-10"); pawn.TakeDamage(attack); } } } if (async != null) { StopCoroutine(StateChange()); } async = StartCoroutine(StateChange()); } } }
/// <summary> /// OnCollisionEnter is called when this collider/rigidbody has begun /// touching another rigidbody/collider. /// </summary> /// <param name="other">The Collision data associated with this collision.</param> void OnCollisionEnter(Collision other) { if (other.gameObject.CompareTag(enemyTag))//碰撞了敌方 { var hit = other.gameObject; if (hit.GetComponent <Pawn1>() != null) { Pawn1 pawn = hit.GetComponent <Pawn1>(); if (pawn != null) { pawn.TakeDamage(attack); } } else if (hit.GetComponent <Pawn2>() != null) { Pawn2 pawn = hit.GetComponent <Pawn2>(); if (pawn != null) { pawn.TakeDamage(attack); } } Destroy(gameObject); } }