Unity3d head name and blood strip update and drawing

Source: Internet
Author: User
Using unityengine; using system. collections; public class NPC: monobehaviour {// main camera object private camera; // NPC name private string name = "I am Yu Song Momo"; // The main object gameobject hero; // float npcheight of the NPC model height; // red blood strip map public texture2d blood_red; // public texture2d blood_black; // default NPC blood value private int HP = 100; void start () {// obtain the principal object hero = gameobject Based on the tag. findgameobjectwithtag ("Player"); // obtain the camera pair Like camera = camera. main; // annotation 1 // get the original height of the model float size_y = collider. bounds. size. y; // get the scaling ratio float scal_y = transform. localscale. y; // their product is the height of npcheight = (size_y * scal_y);} void Update () {// keep the NPC always facing the main character transform. lookat (hero. transform);} void ongui () {// get the coordinates of the NPC head in the 3D world. // by default, the NPC coordinate points are under the soles of the feet, so we can add the height of the npcheight model to vector3 worldposition = new vector3 (transform. position. x, transform. position. Y + NP Cheight, transform. position. z); // convert the 3D coordinates on the head of the NPC to the coordinates vector2 position = camera in the 2D screen. worldtoscreenpoint (worldposition); // obtain the 2D Coordinate Position = new vector2 (position. x, screen. height-position. y); // annotation 2 // calculate the width and height of the bleeding strip vector2 bloodsize = GUI. skin. label. calcsize (New guicontent (blood_red); // calculate the red blood bar display area using the blood value int blood_width = blood_red.width * HP/100; // draw the black blood bar GUI first. drawtexture (New rect (position. X -(Bloodsize. x/2), position. y-bloodsize. y, bloodsize. x, bloodsize. y), blood_black); // draw the red blood bar GUI. drawtexture (New rect (position. x-(bloodsize. x/2), position. y-bloodsize. y, blood_width, bloodsize. y), blood_red); // annotation 3 // calculate the width and height of the NPC name vector2 namesize = GUI. skin. label. calcsize (New guicontent (name); // set the display color to yellow GUI. color = color. yellow; // draw the NPC name GUI. label (New rect (position. x-(namesize. x/2), position. Y-namesize. y-bloodsize. y, namesize. x, namesize. y), name) ;}// The following is the event of the classic Mouse clicking object. You should know what it means. Void onmousedrag () {debug. log ("when you drag the model area");} void onmousedown () {debug. log ("when the mouse is pressed"); If (HP> 0) {HP-= 5 ;}} void onmouseup () {debug. log ("when the mouse is lifted");} void onmouseenter () {debug. log ("when the mouse enters the object area");} void onmouseexit () {debug. log ("when the mouse leaves the model area");} void onmouseover () {debug. log ("when the mouse stays in this object area ");}}

 

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.