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 Latedi · Aug 25, 2012 at 04:20 AM · c#networkupdateongui

How does this script identify other players? C#

So basically I'm following this tutorial for making an FPS game and I have some problems understanding how this script is working. It seems to be doing iterations without any loops to me. Sorry for the long script (Which I hope I managed to put in a code block now).

The script in itself is used to draw small healthbars above the characters' heads if they're in front of the player and if they're more than a unit away or so. It doesn't draw the player's own healthbar either.

However I see no indication in the script of it going through all the player GameObjects, the Update() and OnGUI() functions seem to be using variables from the GameObject containing them.

So yeah if this is readable/understandable and you read through it all, thanks.

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Player label.
 /// 
 /// This script is attached to the player and draws the healthbar of other 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 {
     
     //The healthbar 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 wether 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 healthbar,
     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 adjustmenet = 1;
     
     //Used in displaying the player's name.
     public string playerName;
     private GUIStyle myStyle = new GUIStyle();
     
 
     // Use this for initialization
     void Start ()
     {
         //This script will only run for the other player characters.
         //We don't need a healthbar being drawn above our own avatar in our game.
         if(networkView.isMine == false)
         {
             myTransform = transform;
             myCamera = Camera.main;
             
             //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 wether the player is in front or behind the camera.
         cameraRelativePosition = myCamera.transform.InverseTransformPoint(myTransform.position);
         
         //Figure out how long the healthbar should be and to avoid a healthbar drawing error
         //set the health bar to 1 if the player's 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 above the player.
             worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustmenet,
                                         myTransform.position.z);
             
             //Convert the world position to a point on the screen.
             screenPosition = myCamera.WorldToScreenPoint(worldPosition);
             
             //Draw the healthbar 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);
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Latedi · Aug 25, 2012 at 09:57 PM

Oho I believe I can answer this question myself now. So do correct me if I'm wrong :d

The script is part of all the instantiated player characters, but if the networkView is created/owned(?) by the player the script is disabled. The script will then draw the healthbar above it's transform (which I believe is part of the GameObject player character, or at least the same Vector3 coordinates).

Comment
Add comment · Share
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

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

7 People are following this question.

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

Related Questions

Problem creating GUI.Window, Help please! 1 Answer

Same GUI script showing up in different places 0 Answers

Multiple Cars not working 1 Answer

Convert from C# To Java ????? 1 Answer

Distribute terrain in zones 3 Answers


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