Update the FireBullet() method of Gun.cs to assign bullet's owner.
publicvoidFireBullet(Vector3 position,Vector3 direction,string ownerId) {GameObject bullet =Instantiate(bulletPrefab) asGameObject;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.
publicvoidGotHit(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.