In many RPG games, the dynamic display of the player's information, including the player's name, rank, health value, and the loss of health when attacked, is absolutely indispensable.
Today we will study together how to make. Get up ....
1, first import Ngui, feel Ngui do UI is very convenient.
2, create player player to use cube instead and create sub object under Cube head position default value zero, use: Player information display location.
3, use Ngui to create player information follow, structure as follows:
4, Paste code: NewBehaviourScript.cs Hanging on the Maincamera
usingUnityengine;usingSystem.Collections; Public classNewbehaviourscript:monobehaviour {//role PublicTransform Cube; //to the age lead to move the information blood bar, name PublicTransform UI; //the distance between the default blood bar and the camera Private floatFomat; //role Head Position PrivateTransform Head; voidStart () {//find the character head pointHead = Cube.find ("Head"); //calculate the distance of the default blood barFomat =vector3.distance (head.position,camera.main.transform.position); Debug.Log (Fomat); } voidUpdate () {//Here you can judge if the position has not changed the class no longer assigns a value floatNewfomat = Fomat/vector3.distance (head.position,camera.main.transform.position); Ui.position=Worldtoui (head.position); //calculate the scale of the blood barUi.localscale = Vector3.one *Newfomat; //test code move role 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 coordinates converted to 2D coordinates on the Ngui screen Public StaticVector3 Worldtoui (Vector3 point) {Vector3 pt=Camera.main.WorldToScreenPoint (point); //Uicamera.currentcamerVector3 FF =UICamera.currentCamera.ScreenToWorldPoint (PT); //the z-axis of the UI is 0Ff.z =0; returnFF; }}View Code
5, another script Cube.csgua hangs on the player
usingUnityengine;usingSystem.Collections; Public classCube:monobehaviour { PublicGameobject FG; PrivateGameobject Sprite; Publicgameobject harm; PublicUILabel HP; //Use this for initialization voidStart () {harm. SetActive (false); Hp.text="hp:100/100"; } //Update is called once per frame voidUpdate () {}voidOnMouseDown () {if(FG. Getcomponent<uisprite> () .fillamount>0) {FG. Getcomponent<UISprite> (). Fillamount-=0.1f; Harm. SetActive (true); Hp.text= ((int) (FG. Getcomponent<uisprite> (). fillamount* -)). ToString () +"/100"; } Debug.Log (FG. Getcomponent<UISprite>(). Fillamount); } voidOnMouseUp () {harm. SetActive (false); }}View Code
6, Operation effect:
7, click on the object simulation is attacked, see the amount of blood decreased, and pop the attack value.
Player hung up.
Project Source:
Link: http://pan.baidu.com/s/1jGDpZF4 Password: vh5p
If it doesn't open, check the unity version Oh, I'm testing 4.5.
Player information blood bars and damage values move with the protagonist