zoukankan      html  css  js  c++  java
  • 玩家信息血条及伤害值随主角移动

      许多RPG游戏中绝对少不了的就是玩家信息的动态显示,包括玩家姓名,等级,生命值,被攻击时生命值的减少等。

    今天我们来共同学习一下怎么制作。搞起。。。。

    1,首先导入NGUI,感觉NGUI做UI还是挺方便的。

    2,创建玩家Player可用cube代替且在cube下创建子物体head位置默认值归零,用途:玩家信息显示位置。

    3,使用NGUI创建玩家信息跟随,结构如下:

    4,贴代码:NewBehaviourScript.cs挂到MainCamera上

    using UnityEngine;
    using System.Collections;
    
    public class NewBehaviourScript : MonoBehaviour {
    
        //角色
        public Transform Cube;
        //要岁主角移动的信息 血条,名称
        public Transform UI;
    
        //默认血条与摄像机的距离
        private float Fomat;
        //角色头顶位置
        private Transform Head;
    
        void Start () 
        {
            //找到角色头顶点
            Head = Cube.Find("head");
            //计算一下默认血条的距离 
            Fomat  = Vector3.Distance(Head.position,Camera.main.transform.position);
            Debug.Log (Fomat);
        }
        
        void Update () 
        {
            //这里可以判断一下,如果位置没发生变化课不再赋值
            float newFomat = Fomat / Vector3.Distance(Head.position,Camera.main.transform.position);
            UI.position  = WorldToUI(Head.position);
            //计算血条的缩放比例
            UI.localScale = Vector3.one * newFomat;
    
            //测试代码 移动角色
            if(Input.GetKey(KeyCode.W))
                Cube.Translate(Vector3.forward);
            if(Input.GetKey(KeyCode.S))
                Cube.Translate(Vector3.back);
            if(Input.GetKeyDown(KeyCode.A))
                Cube.Translate(Vector3.left);
            if(Input.GetKeyDown(KeyCode.D))
                Cube.Translate(Vector3.right);
        }
    
    
        //3D坐标转换为NGUI屏幕上的2D坐标
        public static Vector3 WorldToUI(Vector3 point)
        {
            Vector3 pt = Camera.main.WorldToScreenPoint(point);
            //UICamera.currentCamer 
            Vector3 ff = UICamera.currentCamera.ScreenToWorldPoint(pt);
            //UI的Z轴为0
            ff.z = 0;
            return ff;
        }
    }
    View Code

     5,另一个脚本cube.csgua挂在player上

    using UnityEngine;
    using System.Collections;
    
    
    
    public class cube : MonoBehaviour 
    {
        public GameObject fg;
        private GameObject sprite;
        public GameObject harm;
        public UILabel hp;
        // Use this for initialization
        void Start ()
        {
            harm.SetActive(false);
            hp.text = "HP:100/100";
        }
        
        // Update is called once per frame
        void Update () 
        {
            
        }
    
        void OnMouseDown()
        {
            if(fg.GetComponent<UISprite>().fillAmount>0)
            {
                fg.GetComponent<UISprite>().fillAmount -= 0.1f;
                harm.SetActive(true);
                hp.text = ((int)(fg.GetComponent<UISprite>().fillAmount*100)).ToString()+"/100";
            }
    
            Debug.Log (fg.GetComponent<UISprite>().fillAmount);
        }
        void OnMouseUp()
        {
            harm.SetActive(false);
        }
    }
    View Code

    6,运行效果:

    7,点击物体模拟被攻击,看血量减少,并弹出被攻击值。

    玩家挂了

    项目源码:

    链接:http://pan.baidu.com/s/1jGDpZF4 密码:vh5p

    如果打不开,请检查Unity版本哦,本人测试的是4.5.

  • 相关阅读:
    EM算法
    最大熵模型中的对数似然函数的解释
    PySpark 自定义函数 UDF
    PySpark 自定义聚合函数 UDAF
    Numpy总结
    Examples
    Examples
    Examples
    Examples
    Examples
  • 原文地址:https://www.cnblogs.com/wuzhang/p/wuzhang20140924.html
Copyright © 2011-2022 走看看