public void OnHpChanged()
{
// Update the hpSlider when player hp changes
currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();
hpSlider.value = currentHP;
if (currentHP == 0)
{
// invoke the "killed" remote event when hp is 0.
if (networkId.IsMine)
{
remoteEventAgent.Invoke("killed");
}
}
}
public void RemoteKilled()
{
Instantiate(explode, transform.position, Quaternion.identity);
}
Add event listener in the Editor Inspector.
Make sure you applied the changes to the Player Prefab
Respawning the Player
In the GameSceneManager.cs script, add the following methods to respawn the player.
public void DelayedRespawnPlayer()
{
StartCoroutine(RespawnPlayer(1f));
}
IEnumerator RespawnPlayer(float delayTime)
{
yield return new WaitForSeconds(delayTime);
// Respawn the player at a random SpawnPoint
int spawnPointIndex = Random.Range(0, 3);
NetworkClient.Instance.LastSpawner.SpawnForPlayer(0, spawnPointIndex);
}
In the PlayerHP.cs script, update the RemoteKilled() method.
public void RemoteKilled()
{
Instantiate(explode, transform.position, Quaternion.identity);
// Only the source player GameObject should be respawned.
// SceneSpawner will handle the remote duplicate creation for the respawned player.
if (networkId.IsMine)
{
GameSceneManager gameSceneManager = FindObjectOfType<GameSceneManager>();
// Call the DelayedRespawnPlayer() method you just added to the GameSceneManager.cs script.
gameSceneManager.DelayedRespawnPlayer();
// Ask the SceneSpawner to destroy the gameObject.
// SceneSpawner will destroy the local Player and its remote duplicates.
NetworkClient.Instance.LastSpawner.DestroyGameObject(gameObject);
}
}
Play
You can follow the steps in section "Test and Play" to test out player respawn.