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
  • Tracking Bullets Owner
  • Score
  • Displaying Winner and GameOver Panel
  • Exiting the Room
  • Test and Play

Was this helpful?

  1. Tutorials
  2. Third-Person Shooter

Winning the Game

5 - 10 minutes read

Tracking Bullets Owner

In the PlayerWeapon.cs script, update the FireBullet() and RemoteFire() methods to also send/receive playerId.

    void FireBullet()
    {
        SWNetworkMessage msg = new SWNetworkMessage();
        msg.Push(launchPosition.position);
        msg.Push(transform.forward);
        msg.PushUTF8ShortString(NetworkClient.Instance.PlayerRemoteId);
        remoteEventAgent.Invoke("fire", msg);
    }

    public void RemoteFire(SWNetworkMessage msg)
    {
        Vector3 position = msg.PopVector3();
        Vector3 direction = msg.PopVector3();
        string ownerId = msg.PopUTF8ShortString();
        gun.fireBullet(position, direction, ownerId);
    }

Add a property to the Bullet.cs script to track its owner.

public string ownerId;

And update its OnCollisionEnter method to pass its ownerId to the PlayerHP object.

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            PlayerHP ps = collision.gameObject.GetComponent<PlayerHP>();
            if (ps != null)
            {
                ps.GotHit(damage, ownerId);
            }
        }

        foreach (ContactPoint contact in collision.contacts)
        {
            Debug.Log(contact.thisCollider.name + " hit " + contact.otherCollider.name);
            Instantiate(explode, contact.point, Quaternion.identity);
        }
    }

Update the FireBullet() method of Gun.cs to assign bullet's owner.

    public void FireBullet(Vector3 position, Vector3 direction, string ownerId)
    {
        GameObject bullet = Instantiate(bulletPrefab) as GameObject;

        Bullet b = bullet.GetComponent<Bullet>();
        b.ownerId = ownerId;

        bullet.transform.position = position;
        bullet.GetComponent<Rigidbody>().velocity = direction * bulletSpeed;
    }

Score

In the PlayerHP.cs script, update the GotHit() method to take the ownerId string parameter and call the PlayerScored() method of GameSceneManager.cs to update the player's score.

    public void GotHit(int damage, string ownerId)
    {
        // Only the source GameObject can modify the "hp" SyncProperty.
        if (networkId.IsMine)
        {
            currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();

            // Check if the player is already dead.
            if (currentHP == 0)
            {
                return;
            }

            Debug.Log("Got hit: bullet owner= " + ownerId);

            Debug.Log("Got hit: old currentHP= " + currentHP);

            if (currentHP > 0)
            {
                currentHP = currentHP - damage;

                // if hp is lower than 0, set it to 0.
                if (currentHP < 0)
                {
                    currentHP = 0;
                }

                if (currentHP == 0)
                {
                    // call the PlayerScored() method if player hp reached 0. 
                    // GameSceneManager will update the player's score.
                    GameSceneManager gameSceneManager = FindObjectOfType<GameSceneManager>();
                    gameSceneManager.PlayerScored(ownerId);
                }
            }

            Debug.Log("Got hit: new currentHP= " + currentHP);

            // Apply damage and modify the "hp" SyncProperty.
            syncPropertyAgent.Modify("hp", currentHP);
        }
    }

Displaying Winner and GameOver Panel

In the GameSceneManager.cs script, add a method to handle the OnChanged Event for the "PlayerScores" SyncProperty.

We look for a score that is larger than 3 and display the Winner/GameOver panel accordingly.

public void OnPlayerScoresChanged()
{
    Debug.Log("OnPlayerScoreChanged");
    PlayerScores playerScores = roomPropertyAgent.GetPropertyWithName("PlayerScores").GetValue<PlayerScores>();
    Debug.Log(playerScores);

    if(playerScores != null && playerScores.scores != null)
    {
        foreach (Score s in playerScores.scores)
        {
            if(s.score >= 3)
            {
                if (s.playerRemoteId == NetworkClient.Instance.PlayerRemoteId)
                {
                    winnerPanel.gameObject.SetActive(true);
                }
                else
                {
                    gameOverPanel.gameObject.SetActive(true);
                }
                break;
            }
        }
    }
}

Exiting the Room

Update the Exit() method in the GameSceneManager.cs script to disconnect and leave the room.

DisconnectFromRoom() disconnects the player from the game servers. All the SWNetwork GamePlay components will stop working.

LeaveRoom() removes the player from its current room in the lobby.

    public void Exit()
    {
        NetworkClient.Instance.DisconnectFromRoom();
        NetworkClient.Lobby.LeaveRoom(HandleLeaveRoom);
    }

    void HandleLeaveRoom(bool okay, SWLobbyError error)
    {
        if (!okay)
        {
            Debug.LogError(error);
        }

        Debug.Log("Left room");
        SceneManager.LoadScene("lobbyScene");
    }

Test and Play

Now the game is complete. I hope you enjoyed the tutorial!

Congratulations! You have done the tutorial.

PreviousUpdating Room StatesNextBasic Lobby

Last updated 5 years ago

Was this helpful?