LogoLogo
  • SWNetwork SDK Overview
  • Installation
    • Custom Unity Package
  • Tutorials
    • Third-Person Shooter
      • Starter Project Introduction
      • Install SWNetwork
      • Setting up the NetworkClient
      • Sync Player Transform
      • Setting up the Scene Spawner
      • Test and Play
      • Remote Events
      • SyncProperties
      • Player Respawn
      • Updating Room States
      • Winning the Game
    • Basic Lobby
      • Lobby-start
      • Installing SWNetwork SDK and configuring NetworkClient
      • Registering Player
      • Room CRUD
      • Managing Teams
      • Chat
  • SDK
    • Network Client
      • Check-in
      • Game Servers
      • Network Status Event (beta)
      • Classes
        • NetworkClient Class
      • Methods
        • CheckIn() Method
        • ConnectToRoom(Action<bool>) Method
        • DisconnectFromRoom () Method
        • FindSpawner(ushort) Method
    • Game Play
      • Network ID
      • Realtime Agent
      • Remote Event
      • Sync Property
        • Restore Sync Properties
        • Conflict Resolution
      • SceneSpawner
      • RoomPropertyAgent
      • RoomRemoteEventAgent
    • Lobby
      • Register Player
      • Message Player
      • Create Room
      • Change Room Settings
      • Get Rooms
      • Filter Rooms
      • Join Room
      • Message Room
      • Get Players in Room
      • Get Room Custom Data
      • Change Room Custom Data
      • Kick Players
      • Start Room
      • Leave Room
      • Lobby Room Events
        • OnLobbyConnectedEvent
        • OnPlayerMessageEvent
        • OnRoomCustomDataChangeEvent
        • OnNewPlayerJoinRoomEvent
        • OnPlayerLeaveRoomEvent
        • OnNewRoomOwnerEvent
        • OnRoomStartingEvent
        • OnRoomReadyEvent
        • OnFailedToStartRoomEvent
        • OnKickedEvent
        • OnRoomMessageEvent
      • Classes
        • SWLobby Class
        • SWPlayer Class
        • SWRoom Class
        • SWRegisterReply Class
        • SWGetRoomReply Class
        • SWJoinRoomReply Class
        • SWGetRoomCustomDataReply Class
        • SWGetPlayersReply Class
        • SWLobbyIndexData Class
        • SWLobbyFilterData Class
        • SWGetRoomFilterReply Class
        • SWLobbyError Class
        • SWMessagePlayerEventData Class
        • SWMessageRoomEventData Class
        • SWRoomCustomDataChangeEventData Class
        • SWJoinRoomEventData Class
        • SWLeaveRoomEventData Class
        • SWRoomChangeOwnerEventData Class
        • SWStartRoomEventData Class
        • SWRoomReadyEventData Class
        • SWFailedToStartRoomEventData Class
      • Methods
        • Register(Action<bool, SWRegisterReply, SWLobbyError>) Method
        • MessagePlayer(string, string, Action<bool, SWLobbyError>) Method
        • CreateRoom(bool, int, Action<bool, string, SWLobbyError>) Method
        • ChangeRoomSettings(int, int, Action<bool, SWLobbyError>) Method
        • GetRooms(int, int, Action<bool, SWGetRoomReply, SWLobbyError>) Method
        • FilterRoom(SWLobbyFilterData, byte, Action<bool, SWGetRoomFilterReply, SWLobbyError>) Method
        • JoinRoom(string, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinRoomRandomly(Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinOrCreateRoom(bool, int, int, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • MessageRoom(string, Action<bool, SWLobbyError>) Method
        • GetRoomCustomData(Action<bool, SWGetRoomCustomDataReply, SWLobbyError>) Method
        • GetPlayersInRoom(Action<bool, SWGetPlayersReply, SWLobbyError>) Method
        • ChangeRoomCustomData(string, Action<bool, SWLobbyError>) Method
        • StartRoom(Action<bool, SWLobbyError>) Method
        • LeaveRoom(Action<bool, SWLobbyError>) Method
  • Open Source Software Used
    • Credits
Powered by GitBook
On this page
  • Player Explosion
  • Respawning the Player
  • Play

Was this helpful?

  1. Tutorials
  2. Third-Person Shooter

Player Respawn

3 - 5 minutes read

As the players shoot each other, their hp reaches 0 and stays at 0. You can add another Remote Event to trigger the player explosion death effect.

Player Explosion

Add another Remote Event and name it "killed".

In the PlayerHP.cs Script:

  1. Add a RemoteEventAgent property and initialize it in the Start() method.

  2. Update the OnHpChanged() method to invoke the "killed" remote event when hp is 0.

  3. Add RemoteKilled() method to handle the "killed" remote event.

 RemoteEventAgent remoteEventAgent;
   void Start()
   {
        hpSlider.minValue = 0;
        hpSlider.maxValue = maxHp;

        networkId = GetComponent<NetworkID>();
        syncPropertyAgent = gameObject.GetComponent<SyncPropertyAgent>();
        remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();
   }
    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.

PreviousSyncPropertiesNextUpdating Room States

Last updated 6 years ago

Was this helpful?