Character Colors
Paul Molnar, May 2022
Overview:
"Oh no! I can't tell who is who in my VR application I'm building in Unity! If only there was a way to help me distinguish my friends from my enemies!"
Wow, is that you talking up there? Luckily I've got just the tutorial for you. Every collaborative multiplayer application needs some way to be able to distinguish between players. Many finished programs use nametags, colors, and custom avatars for each player to give each player their own distinct look. If you are a beginner in Unity, any of these features can seem daunting so this page will go into how to give players the option of changing their color. Color changing can be a hassle in Photon Pun 2 so this page will go over one specific way of changing a network player's color using collisions.
Color Changing System:
The plan for this system is to create color changing blocks around the environment. If the player collides with one of these blocks it takes the color being rendered on the block and changes it's own avatar color to be the same as the block. On the left you can see a simple scene that has 5 color blocks to choose from.
Implementing the System:
The first part of the system and the easiest to implement are the color changing blocks. All you need on them is a renderer component and a sphere collider set to "Is Trigger" (sized to your preference). You will want to make an interesting material to give the renderer. In the image above I have the Emission checkbox selected to give them the extra neon colors. You will also need to add a new tag to this object which you can call whatever you like. I recommend "Changer"
If at this point you haven't followed one of the basic tutorials on creating a multiplayer VR game in Unity using Photon Pun 2 then make sure to go do that through one of these links: the wiki page Tutorial: Steam VR in Unity with Photon Pun 2 , or watching the following three YouTube videos made by Valem: Video 1, Video 2, Video 3. This will give you the proper tools to set up both the player and the network player objects. We will be looking into a script to put on the network player object.
The next part of this tutorial will go into the ideas behind a script. Create a script and put it on your network player. In the OnTriggerEnter(Collider other) function you will check to see if you have entered the trigger sphere around your color changer and if so, change the material of your objects renderer to be the same as the color changer. In the code below the placeholder object is the part of your network player's avatar that you want to change the color of.
if (other.tag == "Changer") {
Material mat = other.gameObject.transform.GetChild(0).gameObject.GetComponent<Renderer>().material;
(PLACEHOLDER OBJECT).GetComponent<Renderer>().material = mat;
}
This is really the only code you need. Now when your network player enters the trigger of a color changer it will change part of its avatar to be the same color.
Possible Extensions:
One way to extend this would be to assign each player a random color in the very beginning of the game and update the network player's material. This would not allow the players to choose their own colors though which could be a convenient feature. Another way to extend this would be to allow players to choose their color through a GUI right when they enter the room. This is a much harder implementation but could have better results and not require movement in order to choose the colors. The last extension would be to add nametags to players so that it could be even easier to see who is who. This is the hardest extension of the code and would require whole new systems to implement. However, if implemented successfully, this could be the most rewarding. For people interested in this topic I recommend trying it and adding a wiki page to allow others to follow your work.