Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skalde · Jul 01, 2013 at 11:01 PM · guimultiplayerpositionhealth

Can't get Gui Pos on top of Enemy ingame!

Hello Unity Community i Have this huge problem.. i'm making an multiplayer game, everything works exept from the enemy's healthbar on top of his head from the other players view.. i can't get the position right!!

Anyways, heres the script:

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// This script is attached to the player and it draws the 
 /// healthbar of players above them.
 /// 
 /// This script accesses the HealthAndDamage script for 
 /// determining the healthbar length.
 /// 
 /// This script is accessed by the PlayerName script.
 /// </summary>
 
 
 public class PlayerLabel : MonoBehaviour {
     
     //Variables Start___________________________________
     
     //The health bar texture is attached to this in the inspector.
     
     public Texture healthTex;
     
     
     //Quick references.
     
     private Camera myCamera;
     
     private Transform myTransform;
     
     private Transform triggerTransform;
     
     private HealthAndDamage HDScript;
     
     
     //These are used in determining whether the healthbar should be drawn
     //and where on the screen.
     
     private Vector3 worldPosition  = new Vector3();
     
     private Vector3 screenPosition  = new Vector3();
     
     private Vector3 cameraRelativePosition = new Vector3();
     
     private float minimumZ = 1.5f;
     
     
     //These variables are used in defining the health bar.
     
     private int labelTop = 18;
     
     private int labelWidth = 110;
     
     private int labelHeight = 15;
     
     private int barTop = 1;
     
     private int healthBarHeight = 5;
     
     private int healthBarLeft = 110;
     
     private float healthBarLength;
     
     private float adjustment = 1;
     
     public Camera Shooter;
     
     
     
     //Used in displaying the player's name.
     
     public string playerName;
     
     private GUIStyle myStyle = new GUIStyle();
 
     //Variables End_____________________________________
     
     
     void Awake ()
     {
         //This script will only run for the other player characters.
         //We don't need a health bar being drawn above our own player in
         //our game.
         
         if(networkView.isMine == false)
         {
             myTransform = transform;
             
             myCamera = Shooter;
             
             
             //Access the HealthAndDamage script.
             
             Transform triggerTransform = transform.FindChild("Trigger");
             
             HDScript = triggerTransform.GetComponent<HealthAndDamage>();            
             
             
             //The font colour of the GUIStyle depends on which team the 
             //player is on.
             
             if(myTransform.tag == "BlueTeam")
             {
                 myStyle.normal.textColor = Color.blue;    
             }
             
             if(myTransform.tag == "RedTeam")
             {
                 myStyle.normal.textColor = Color.red;    
             }
             
             myStyle.fontSize = 12;
             
             myStyle.fontStyle = FontStyle.Bold;
             
             
             //Allow the text to extend beyond the width of the label.
             
             myStyle.clipping = TextClipping.Overflow;
             
         }
         
         else
         {
             enabled = false;    
         }
     }
     
     
     // Update is called once per frame
     void Update () 
     {    
         //Capture whether the player is in front or behind the camera.
         
         cameraRelativePosition = myCamera.transform.InverseTransformPoint(myTransform.position);
         
         
         //Figure out how long the health bar should be and to avoid a mathematical error set
         //the health bar length to 1 if the the player's health falls below 1.
         
         if(HDScript.myHealth < 1)
         {
             healthBarLength = 1;    
         }
         
         if(HDScript.myHealth >= 1)
         {
             healthBarLength = (HDScript.myHealth / HDScript.maxHealth) * 100;    
         }
     }
     
     
     void OnGUI ()
     {
         //Only display the player's name if they are in front of the camera and also the 
         //player should be in front of the camera by at least minimumZ.
         
         if(cameraRelativePosition.z > minimumZ)
         {
             //Set the world position to be just a bit above the player.
             
             worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,
                                         myTransform.position.z);
             
             //Convert the world position to a point on the screen.
             
             screenPosition = myCamera.WorldToScreenPoint(worldPosition);
             
             
             //Draw the health bar and the grey bar behind it.
             
             GUI.Box(new Rect(screenPosition.x - healthBarLeft / 2, 
                              Screen.height - screenPosition.y - barTop,
                              100, healthBarHeight), "");
 
             GUI.DrawTexture(new Rect(screenPosition.x - healthBarLeft / 2,
                                      Screen.height - screenPosition.y - barTop,
                                      healthBarLength, healthBarHeight), healthTex);        
             
             
             //Draw the player's name above them.
             
             GUI.Label(new Rect(screenPosition.x - labelWidth / 2,
                                Screen.height - screenPosition.y - labelTop,
                                labelWidth, labelHeight), playerName, myStyle);
 
         }
     }    
 }


I'v got no errors, the "bar" just flies around like crazy.

please help! Thanks in advance.

Skalde

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Players Health wont work?!? PLEASE HELP 1 Answer

Can you make a GUI.Box follow a player's position?(SOLVED) 7 Answers

Help With Enemy Health bars 3 Answers

GUI Texture Positioning Help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges