敌人死亡

当敌人受到主角的攻击时,血量会减少,直至死亡。

像LOL英雄或者小兵死亡后会倒在地上,一段时间后尸体不见(被销毁),本节实现类似的功能:敌人死亡,几秒后移入地下,几秒后敌人尸体被销毁。

//using UnityEngine;
//using System.Collections;
//
//public class Enemy : MonoBehaviour {
//	public static Enemy _instance;
//
//	public GameObject damageEffectPrefab;// 受到攻击时的出血特效
//	public int hp = 200;
//
//	public Transform bloodPoint;// 敌人受伤时候的出血点位置的Transform组件
//
//	public float speed = 2f;// 敌人行走的速度
//	private CharacterController cc; // 定义控制敌人行走的CharacterController组件
//
//	// **********
//	public float attackRate = 2;      // 攻击速率,2s攻击一次
//	private float attackDistance = 2; // 敌人发生攻击需要的距离
//	private float distance = 0;       // 敌人和主角的距离
//	private float attackTimer = 0;
//
//	private float downSpeed = 1;
//	private float downDistance = 0;
//
//
//	void Awake(){
//		_instance = this;
//	}
//
//
//	void Start(){
//		cc = this.GetComponent ();//  得到CharacterController组件
//		// 通过协同来计算距离,以优化在Update中逐帧调用CalcDistance而消耗的性能
//		InvokeRepeating("CalcDistance", 0, 0.1f);
//	}
//
//	void Update(){
// ***************************************************************************************
		if (hp <= 0) {// true==>敌人死亡
			// 移到地下
			transform.Translate(-transform.up * downSpeed * Time.deltaTime);
			downDistance += downSpeed * Time.deltaTime;// 下落的距离
			if(downDistance > 3/*敌人死亡后下落超过3m后销毁死的敌人,节约性能*/){
				GameObject.Destroy (this.gameObject);
			}
			return;
		}
// ***************************************************************************************


//		if (distance < attackDistance) {// true==>敌人攻击
//			attackTimer += Time.deltaTime;
//			if (attackTimer > attackRate) {
//				// 攻击之前面向主角
//				// 本代码中获取主角调用多次,可以在Start反方中进行初始化以进行优化,这里仅供学习用,方便清楚代码整个框架(逻辑)
//				Transform player = TranscriptManager._instance.player.transform;
//				Vector3 targerPos = player.position;
//				targerPos.y = transform.position.y;
//				transform.LookAt(targerPos);
//				// **************************************
//				GetComponent().Play("attack01");// 其中attack01为敌人攻击的动画
//				attackTimer = 0;
//			}
//
//			if (!GetComponent ().IsPlaying ("attack01")) {
//				GetComponent ().CrossFade ("idle");
//			}
//
//		} else {
//			GetComponent ().Play ("walk");// 播放行走的动画
//			Move ();// 在Update方法中,每一帧调用Move方法
//		}
//
//	}

//	// *******************计算距离***********************************************
//	void CalcDistance(){
//		Transform player = TranscriptManager._instance.player.transform;
//		distance = Vector3.Distance (player.position, this.transform.position);
//	}
//
//
//	// *********************** 控制敌人AI行走的方法 *********************************
//	void Move(){
//		// 需求分析:要实现敌人总是朝着游戏主角行走
//
//		// 1. 首先获取主角相关组件
//		// 主角的Transform组件
//		Transform player = TranscriptManager._instance.player.transform;// 获取主角由TranscriptManager类管理
//		Vector3 targerPos = player.position;
//		targerPos.y = transform.position.y;// 敌人的y和主角的y一致
//
//		// 2. 面向主角
//		transform.LookAt(targerPos); // this/*敌人*/.transform.LookAt(targetPos)
//
//		// 3. 敌人移动
//		cc.SimpleMove (transform.forward * speed);
//
//	}
//
//	// ********* 受到攻击就调用该方法 *************
//	// 0. 受到多少伤害
//	// 1. 浮空和后退的距离
//	void TakeDamage(string args){
//		if (hp <= 0)
//			return;
//		string[] proArray = args.Split (',');
//
//		// 减去伤害值
//		int damage = int.Parse(proArray[0]);// 获取某种攻击的伤害值,比如普通攻击
//		hp -= damage;
//		// 受到攻击的动画
//		Debug.Log("takedamage");
//		GetComponent().Play ("takedamage");
//
//		// ******浮空和后退*****
//		float backDistance  = float.Parse(proArray[1]);
//		print (backDistance);
//		float jumpHeight = float.Parse(proArray[2]);
//		print (jumpHeight);
//		// 当主角攻击敌人时,敌人向后移动,依然使用iTween
//		iTween.MoveBy(this.gameObject, 
//			transform.InverseTransformDirection(TranscriptManager._instance.player.transform.forward)*backDistance + Vector3.up*jumpHeight,
//			0.3f);
//		// 出血特效,首先获取特效,定义public变量并赋值
//		/*  -----------把敌人受伤时的音效挂在这个damageEffectPrefab上岂不是更好,在Inspector面板设置----------- */
//		GameObject.Instantiate(damageEffectPrefab, bloodPoint.position,Quaternion.identity);
//
//		print ("take damage");// ****************************
//
//		// 死亡
//		if(hp <= 0){
//			Dead ();
//			return;
//		}
//
//	}
//	// 当敌人死亡时调用该方法
//	void Dead(){
//		GetComponent ().Play ("die");
//	}
//}



你可能感兴趣的:(Unity学习札记)