主角连击敌人处理:连击次数的显示(图片数字字体)---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"); // } //}
效果
相关文章推荐
- 学习lamda表达式
- http://blog.csdn.net/samxx8/article/details/11138189
- CoreAnimation 核心动画
- java后端受管bean数据转化为JSON格式传到前端javaScript中
- NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)
- 文件实现通讯录修改版
- VS CODE
- 数据结构笔记整理第7章:排序
- 数据库连接池libzdb的使用
- C#三层结构双数据源复习
- kbengine引擎的安装
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
- libzdb简介
- Leetcode_c++: Search a 2D Matrix (074)
- SpringBoot操作MongoDB实现分页查询
- C# 调用 C++ 在Release下重新生成的问题
- libzdb
- 在OS X 10.10系统上安装Navicat Premium中文破解版11.0.16教程
- java基础理解(重载与重写--方法与类的分类等)
- [SM] 奇迹元素