Player - Capi-Metaverse/Template GitHub Wiki
The Player spawn and logic is closely linked to how Photon Fusion works, so in this page we will explain how to connect to the Photon Network and what elements are needed to do that.
The entry point is the Network Runner Handler that was mentioned in the Scene Management page. This component connects the user to the Photon Network and instantiates the Network Runner that is the GameObject that is going to maintain the user connected to the network and send information about him across the metaverse room.
The Network Runner is inside a prefab called Network Runner Prefab. This GameObject has importants scripts to the Player Character as the Spawner, Photon Voice and the Voice Recorder.
For this page, we will talk about the Spawner and all the scripts and prefabs related to them.
The Spawner inherits from the MonoBehaviour to be assigned to a GameObject and from INetworkRunnerCallbacks, an interface from Photon Fusion with some callbacks called when the user joins the room, leaves the room…
We will explain the most important methods from this class:
-
On Player Join: In this method, the user spawns his avatar and with the Spawn function from the Runner. This method accepts some arguments like the prefab to spawn, the position and a initialization method, that in our case is OnBeforeSpawn. This method called to the PlayerList to initialized it.
-
OnBeforeSpawn: This method set the nickname of the user, his chosen avatar and his ActorID, that is the ID of the player inside the actual room.
-
OnInput: It gets the Input for all the players of the room to coordinate their movement in the CharacterControllerCustom Class.
The character Prefab is a bit complex because he had a lot of GameObjects so we are going to explain them one by one:
-
PlayerCamera: This is the main camera of the character. It will be the one that the user see.
-
CameraAnchorPoint: This GameObject is an anchor for the camera. Once the spawn is completed, the camera is separated from the character gameobject and put in the scene to be easier to manipulate.
-
PlayerUI Prefab: This contains the UI for the camera like the crosshair, the mic icon…
-
NameUI: This UI is above the head of the character and contains his nickname.
-
Speaker: GameObject needed to get the voice from the mic of the user.
-
EmoteWheel: This GameObject contains the emote wheel.
This prefab will be spawned in the On Player Join method from the Spawner. The main scripts for this prefab are the following:
-
Network Player: This script add the avatar to the GameObject and initializes all their properties for the other players.
-
Character Input Handler: This script gets the inputs from the keys and call their respective logic.
-
Character Movement Handler: This script gets the movement data from the Runner and calls the move method from NetworkCharacterController.
-
NetworkCharacterControllerCustom: This scripts contains the functions that let users move, jump and coordinates the walking and running animations.