Skip to main content

Displaying Enemy ID above the player

You can grab a sample template to work with from Github.

First we will add text above a player. This text will always be directly attached to the player. This mean that when the player rotates the text rotates with them. This means it's not the most useful for quickly identifying players. But it is a quick way to get text into a scene or onto a prefab.

Static Text above a player

Open the player prefab

Add a 3D TextMeshPro object

GameObject > 3D Object - Text - TextMeshPro

Use the scale tool to resize the text box and the inspector to change the properties and font size of the text.

When you run the game you can see the text above each player.

Note the player controller on this character doesn't rotate the player so the text always appears ok.

Change the rotation of a player and look at the player in the scene. The text won't be right

Now we will link up the textbox to display the player ID.

Open up the PlayerUIMananger script.

Add a TextMEshPro variable (Line 11) for linking the text that is above the player.

public TextMeshPro playerTextAbove;

On line 18 repeat the code used previously for line 17 to set the text on the text object playerTextAbove.text = "Player " + playerID;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class PlayerUIManager : MonoBehaviour
{
public int playerID;
public TextMeshProUGUI playerText;
public TextMeshProUGUI playerTextAbove;
// Start is called before the first frame update
void Start()
{
playerID = GetComponent<PlayerInput>().playerIndex + 1;
playerText.text = "Player " + playerID;
playerTextAbove.text = "Player " + playerID;
}

}

Select the Player prefab.

Drag the text mesh object to the variable slot in the inspector.

Test the game, you now have a player with text above the player prefab (albeit with some problems)