WASD control UI Interface Blood bar plus minus

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.