Local Multiplayer (New Input System)

Displaying player ID on the UI/HUD

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 10 minutes

This lesson will show how to use UI elements to display player information on the screen.

Download the Starter Template if you don’t have a Unity Project[4] to use.

You will need[5] to add a Player Input Manager to the scene and then link the player prefab.

We want each player to eventually have their own Canvas / UI.

If we want one UI for the entire scene apply this to the Scene instead.

Creating the UI/HUD

To achieve this we need to open the player prefab.

Next insert a Canvas object onto the prefab.

So long as the UI is not interactable (clickable) we do not need to add an event[6] system[7].

Add a Text – TextMeshPro object to the scene.

When you first add a Text Mesh Pro object you will be asked to import TMP Essentials. Click the Import TMP Essentials button. If you do not Text Mesh Pro objects will not work.

By default an EventSystem is often added.

As we are using the multiplayer system we don’t want this.

If you added the Canvas and Text Mesh Pro separately you might not have the EventSystem. It will also add one by default if you are working directly on the scene.

Change the text in the inspector and the Vertex Color.

Now we need to ensure[8] that the Canvas objects are inside the canvas.

In the player adjust the resolution to match your target output for the game.

Adjust the settings until you get the positions correct

When positioning objects look at Rect Transform and click the grid in the top left.

You can then set the origin point for that item in relation to the page

You can also adjust the height and width of the objects here.

Play the game regularly to test the UI.

Note when we add two players it appears that the second player UI hasn’t appeared.

If we look in the hierarchy we can see that the ui is overlapping.

You can see this by moving one Text box in the Scene view.

Individual Player UI/HUD

Go back to the Player prefab.

Select the Canvas

Go to the inspector and change the screen space for the Canvas to Screen Space – Camera

Now we need to link the camera of the prefab to the scene.

Drag the camera to Render Camera or click the target and select the camera.

Now when we test each player gets their own UI.

Note that the UI hasn’t scaled.

Change the Canvas Scaler UI Scale Mode to Scale With Screen Size

You will also want to change the reference resolution to match your target resolution.

I suggest 1920 x 1080 for Full HD.

We need to create a script to update the text on the canvas to display the player id.

Create a Script called PlayerUIManager and attach it to the Player prefab.

Open the script.

As we are gathering information from the playerinput (player controls) and the text mesh pro package TMPro.

This is shown on lines 4 and 5.

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

public class PlayerUIManager : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    
    }

}

Next set up two variables[1].

One int to store the player id, shown on line 9.

Line 10 has us create a TextMeshProuGUI object that will be linked to the text object on the canvas. We will link this after completing the code for this script.

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

public class PlayerUIManager : MonoBehaviour
{
    public int playerID;
    public TextMeshProUGUI playerText;
    // Start is called before the first frame update
    void Start()
    {
    
    }

}

Inside the Start() method we need to setup the playerID and also update the playerText.

Line 14 gets the PlayerInput component[2] of the object this script is attached to. we get the playerIndex value and store this in the playerID variable[3]. We add + 1 to it to update the player number.

On line 15 we update the text by accessing the TextMeshUGUI object through playerText.text and then assign the message to the text area. This is achieved with “Player ” + playerID. We use

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

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

}

Save the script and run the game.

Terms definitions
2. component. The three strands of Technology in the NZC (Technological Practice[9], Technological Knowledge, and Nature of Technology) – together with the six specialist knowledge and skills strands for levels 6, 7, and 8) – support[11] the development of students’ technological literacy[10]. Each of these strands is broken down into smaller groupings (or substrands) called “components”. For […]
3. variable. A placeholder for a piece of information that can change.
4. Project. A series of tasks that need to be completed in order to reach a specific outcome[13]
5. need. An identified requirement of a person, group, or environment[12]. A need is identified from an issue[14] and sits within a context[15]. Technological practice[9] can be undertaken in an attempt to meet an identified need.
Terms definitions
1. variables. Length: 10 minutes This lesson will show how to use UI elements to display player information on the screen. Download the Starter Template if you don’t have a Unity Project to use. You will need to add a Player Input Manager to the scene and then link the player prefab. We want each player to […]
2. component. Length: 10 minutes This lesson will show how to use UI elements to display player information on the screen. Download the Starter Template if you don’t have a Unity Project to use. You will need to add a Player Input Manager to the scene and then link the player prefab. We want each player to […]
3. variable. Length: 10 minutes This lesson will show how to use UI elements to display player information on the screen. Download the Starter Template if you don’t have a Unity Project to use. You will need to add a Player Input Manager to the scene and then link the player prefab. We want each player to […]
4. Project. Length: 10 minutes This lesson will show how to use UI elements to display player information on the screen. Download the Starter Template if you don’t have a Unity Project to use. You will need to add a Player Input Manager to the scene and then link the player prefab. We want each player to […]
5. need. Length: 10 minutes This lesson will show how to use UI elements to display player information on the screen. Download the Starter Template if you don’t have a Unity Project to use. You will need to add a Player Input Manager to the scene and then link the player prefab. We want each player to […]