您的位置:首页 > 其它

主角连击敌人处理:连击次数的显示(图片数字字体)---1

2016-06-03 00:43 295 查看

需求分析

实现的功能:当主角连击敌人时,显示连击提示以及连击的次数。

具体实现

1. 利用NGUI在Unity 2D模式下创建UISprite(资源为Atlas文件夹下的Combo贴图),并命名为BgCombo。然后,在BgCombo下创建UILabel作为Child,并命名为numberLabel。



2. UILabel的字体为上篇博文中制作的数字字体Prefab;设置相关参数,如下图所示:



3. 给BgCombo添加脚本:Combo.cs,代码如下:

using UnityEngine;
using System.Collections;

/** Noted by @BigoSprite 20160603
* 需求分析:当主角连续攻击敌人时会出现“连击的提示”。
* 实现方案:当敌人受到攻击时,需要调用ComboPlus()以显示连击数的提示
* 			 敌人受到伤害是在Enemy脚本中
*
* 注意:该脚本挂在连击Combo这个Sprite上
*/

public class Combo : MonoBehaviour {
public static Combo _instance; // 单例模式
public float comboTime = 2;    //  连击时间
private int comboCount = 0;    // 连击数
private float timer = 0;

private UILabel numberLable; // 控制连击次数的UILabel

void Awake () {
_instance = this;
numberLable = transform.Find ("numberLabel").GetComponent<UILabel> ();
// 没连击时,默认不显示
this.gameObject.SetActive(false);
}

// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
if (timer <= 0) {// 计时器<=0==>不连击了
this.gameObject.SetActive (false);
comboCount = 0;
}

}

// 控制连击的方法
public void ComboPlus(){
this.gameObject.SetActive (true);// 发生连击时,把Combo显示出来
timer = comboTime;
comboCount++;
// UILabel中的text属性控制数字的显示
numberLable.text = comboCount.ToString ();
}
}
4. 在Enemy脚本中,当敌人受到伤害时(这时主角攻击了敌人),调用ComboPlus方法,以显示连击及其次数;代码如下:

//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> ();//  得到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<Animation>().Play("attack01");// 其中attack01为敌人攻击的动画
//				attackTimer = 0;
//			}
//
//			if (!GetComponent<Animation> ().IsPlaying ("attack01")) {
//				GetComponent<Animation> ().CrossFade ("idle");
//			}
//
//		} else {
//			GetComponent<Animation> ().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;
// ----- 受到攻击时,须显示连击数 -------
Combo._instance.ComboPlus();
//
//
//
//		string[] proArray = args.Split (',');
//
//		// 减去伤害值
//		int damage = int.Parse(proArray[0]);// 获取某种攻击的伤害值,比如普通攻击
//		hp -= damage;
//		// 受到攻击的动画
//		Debug.Log("takedamage");
//		GetComponent<Animation>().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<Animation> ().Play ("die");
//	}
//}

效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: