public void Takedamage(int damage){ if (wolfstatexu == Wolfstate.Death) return; //攻击主角目标 targetPlayer = GameObject.FindGameObjectWithTag (Tags.player).transform; wolfstatexu = Wolfstate.Attack; float value = Random.Range (0.0f, 1.0f); if (value < miss_rate) {//miss AudioSource.PlayClipAtPoint(misssong,transform.position); hudtext.Add("Miss",Color.blue,1); } else { hudtext.Add("-"+damage,Color.yellow,1); this.hp-=damage; //显示红色被攻击状态 StartCoroutine(ShowRedAttack()); if(hp<=0){ wolfstatexu=Wolfstate.Death; Destroy(this.gameObject,2);//2s之后消失 } } }
void autoAttack(){ PlayerAttackState playerstate = targetPlayer.GetComponent<PlayerAttack> ().playerattackxu; if (playerstate == PlayerAttackState.Death) { targetPlayer=null; wolfstatexu=Wolfstate.Idle; return; } if (targetPlayer != null) { float distancexu=Vector3.Distance(targetPlayer.position,transform.position); if(distancexu<=mindistance){ //自动攻击 attack_time+=Time.deltaTime;//完成最后一帧所需时间 animation.CrossFade(wolf_nowAttack); if(wolf_nowAttack==wolf_normalAttack){ if(attack_time>wolfnormal_time){ //产生伤害 targetPlayer.GetComponent<PlayerAttack>().TakeDamage(wolf_damage); wolf_nowAttack=wolf_idle; } }else if(wolf_nowAttack==wolf_crazyAttack){ if(attack_time>wolfcrazy_time){ //产生伤害 targetPlayer.GetComponent<PlayerAttack>().TakeDamage(wolf_damage); wolf_nowAttack=wolf_idle; } } if(attack_time>1/attack_rate){ //再次攻击 RandomAttack(); attack_time=0; } } else if(distancexu>maxdistance){ targetPlayer=null; wolfstatexu=Wolfstate.Idle; } else{ //朝向目标 transform.LookAt(targetPlayer.position); cc.SimpleMove(transform.forward*speed); animation .CrossFade(wolf_walk); // wolfstatexu=Wolfstate.Walk; } } else { wolfstatexu=Wolfstate.Idle; } }