usingUnityengine;usingSystem.Collections;usingUnityengine.ui; Public classHealthcontroller:monobehaviour {//is the current object a blood bar or a blue stripe ? Public BOOLIshealth =false; //virtual axis, horizontal axis, vertical shaft Private floatHor,ver; //maximum width of the blood bar Private floatHealthmaxwidth =470; //maximum width of the blue bar Private floatPowermaxwidth =425; //the current virtual axis Private floatCurrentaxis; //the current maximum width Private floatCurrentmaxwidth; //Recttransform Components PrivateRecttransform recttr; //Speed Public floatSpeed = -; //Blood Bar text PublicText Healthtext; //Blue Bar Text PublicText PowerText; //maximum value of the blue bar of a blood bar Public floatHealthmaxvalue =1000f; Public floatPowermaxvalue =500f; //maximum value of the current bar Private floatCurrentmaxvalue; //the currently modified text display box PrivateText Currenttext; voidStart () {recttr= getcomponent<recttransform> (); } voidUpdate () {Hor= Input.getaxis ("Horizontal"); Ver= Input.getaxis ("Vertical"); //if it's a blood bar, if(ishealth) {Currentaxis=Hor; Currentmaxwidth=Healthmaxwidth; Currenttext=Healthtext; Currentmaxvalue=Healthmaxvalue; } //if it's a blue stripe, Else{Currentaxis=ver; Currentmaxwidth=Powermaxwidth; Currenttext=PowerText; Currentmaxvalue=Powermaxvalue; } //calculates the width of the current bar according to the user's actions floatNewwidth = recttr.sizedelta.x +Currentaxis* Time.deltatime *Speed ; //Limit width floatRealwidth = Mathf.clamp (Newwidth,0, Currentmaxwidth); //assigning values to RecttransformRecttr.sizedelta =NewVector2 (REALWIDTH,RECTTR.SIZEDELTA.Y); //Calculate Current Value floatCurrentValue = (realwidth/currentmaxwidth) *Currentmaxvalue; //go to integer operationCurrentValue =Mathf.round (CurrentValue); //displays numeric information to the textCurrenttext.text = currentvalue.tostring () +"/"+currentmaxvalue.tostring (); }}
This script is hung on the bar and the blue bar object
WASD control UI Interface Blood bar plus minus