- Home /
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);
}
}
}
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).
Your answer
Follow this Question
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