From a5d90157458ee75ae4a381aee21c20f4c605a0fc Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Mon, 6 Mar 2023 16:19:05 -0500 Subject: [PATCH 1/3] player and enemy bullet send client rpcs to spawn hit FX --- Basic/Invaders/Assets/Scripts/EnemyBullet.cs | 39 +++++++++++---- Basic/Invaders/Assets/Scripts/PlayerBullet.cs | 49 +++++++++++++------ 2 files changed, 62 insertions(+), 26 deletions(-) diff --git a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs index 66e37fff6..c6d52a29b 100644 --- a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs +++ b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs @@ -1,9 +1,9 @@ -using Unity.Mathematics; +using System; using Unity.Netcode; using UnityEngine; using UnityEngine.Assertions; -public class EnemyBullet : MonoBehaviour +public class EnemyBullet : NetworkBehaviour { private const float k_YBoundary = -4.0f; @@ -16,10 +16,19 @@ public class EnemyBullet : MonoBehaviour [SerializeField] ParticleSystem m_ShieldExplosionParticle; + void Awake() + { + enabled = false; + } + + public override void OnNetworkSpawn() + { + enabled = IsServer; + } + private void Start() { Assert.IsTrue(InvadersGame.Singleton); - Assert.IsTrue(NetworkManager.Singleton); if(InvadersGame.Singleton) InvadersGame.Singleton.isGameOver.OnValueChanged += OnGameOver; @@ -27,44 +36,54 @@ private void Start() private void Update() { - if (!NetworkManager.Singleton.IsServer) return; + if (!IsServer) return; transform.Translate(0, -m_TravelSpeed * Time.deltaTime, 0); if (transform.position.y < k_YBoundary) Destroy(gameObject); } - private void OnDestroy() + public override void OnDestroy() { + base.OnDestroy(); if (InvadersGame.Singleton) InvadersGame.Singleton.isGameOver.OnValueChanged -= OnGameOver; } private void OnTriggerEnter2D(Collider2D collider) { - if (!NetworkManager.Singleton.IsServer) + // several OnTriggerEnter2D calls may be invoked in the same frame (for different Colliders), so we check if + // we're spawned to make sure we don't trigger hits for already despawned bullets + if (!IsServer || !IsSpawned) return; var hitPlayer = collider.gameObject.GetComponent(); if (hitPlayer != null) { hitPlayer.HitByBullet(); - Destroy(gameObject); + NetworkObject.Despawn(); return; } var hitShield = collider.gameObject.GetComponent(); if (hitShield != null) { + SpawnExplosionVFXClientRpc(transform.position, Quaternion.identity); + Destroy(hitShield.gameObject); - Destroy(gameObject); - Instantiate(m_ShieldExplosionParticle, transform.position, quaternion.identity); + NetworkObject.Despawn(); } } + + [ClientRpc] + void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation) + { + Instantiate(m_ShieldExplosionParticle, spawnPosition, spawnRotation); + } private void OnGameOver(bool oldValue, bool newValue) { enabled = false; // On game over destroy the bullets - if (NetworkManager.Singleton.IsServer) Destroy(gameObject); + if (IsServer) NetworkObject.Despawn(); } } diff --git a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs index 25a2e76fe..734dd3266 100644 --- a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs +++ b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs @@ -1,8 +1,8 @@ -using Unity.Mathematics; +using System; using Unity.Netcode; using UnityEngine; -public class PlayerBullet : MonoBehaviour +public class PlayerBullet : NetworkBehaviour { private const float k_YBoundary = 15.0f; public PlayerControl owner; @@ -15,20 +15,33 @@ public class PlayerBullet : MonoBehaviour [SerializeField] ParticleSystem m_EnemyExplosionParticle; + void Awake() + { + enabled = false; + } + + public override void OnNetworkSpawn() + { + enabled = IsServer; + } + private void Update() { - if (!NetworkManager.Singleton.IsServer) return; + if (!IsServer) return; transform.Translate(0, m_TravelSpeed * Time.deltaTime, 0); if (transform.position.y > k_YBoundary) - if (NetworkManager.Singleton.IsServer) - Destroy(gameObject); + { + NetworkObject.Despawn(); + } } private void OnTriggerEnter2D(Collider2D collider) { - if (!NetworkManager.Singleton.IsServer) + // several OnTriggerEnter2D calls may be invoked in the same frame (for different Colliders), so we check if + // we're spawned to make sure we don't trigger hits for already despawned bullets + if (!IsServer || !IsSpawned) return; var hitEnemy = collider.gameObject.GetComponent(); @@ -36,17 +49,13 @@ private void OnTriggerEnter2D(Collider2D collider) { owner.IncreasePlayerScore(hitEnemy.score); - if (NetworkManager.Singleton.IsServer) - { - // Only the server can despawn a NetworkObject - hitEnemy.NetworkObject.Despawn(); - } + // Only the server can despawn a NetworkObject + hitEnemy.NetworkObject.Despawn(); + + SpawnExplosionVFXClientRPC(transform.position, Quaternion.identity); - Destroy(gameObject); + NetworkObject.Despawn(); - // this instantiates at the position of the bullet, there is an offset in the Y axis on the - // particle systems in the prefab so it looks like it spawns in the middle of the enemy - Instantiate(m_EnemyExplosionParticle, transform.position, quaternion.identity); return; } @@ -54,7 +63,15 @@ private void OnTriggerEnter2D(Collider2D collider) if (hitShield != null) { Destroy(hitShield.gameObject); - Destroy(gameObject); + NetworkObject.Despawn(); } } + + [ClientRpc] + void SpawnExplosionVFXClientRPC(Vector3 spawnPosition, Quaternion spawnRotation) + { + // this instantiates at the position of the bullet, there is an offset in the Y axis on the + // particle systems in the prefab so it looks like it spawns in the middle of the enemy + Instantiate(m_EnemyExplosionParticle, spawnPosition, spawnRotation); + } } From 0b542c7c0e867d11155b02f52cbb7ca83676baa0 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Tue, 7 Mar 2023 16:30:08 -0500 Subject: [PATCH 2/3] redundant server side checks removed, spawn/despawn method usage parity --- Basic/Invaders/Assets/Scripts/EnemyBullet.cs | 32 ++++++++++++------- Basic/Invaders/Assets/Scripts/PlayerBullet.cs | 2 -- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs index c6d52a29b..ab07b8879 100644 --- a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs +++ b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs @@ -24,28 +24,36 @@ void Awake() public override void OnNetworkSpawn() { enabled = IsServer; - } - private void Start() - { + if (!IsServer) + { + return; + } + Assert.IsTrue(InvadersGame.Singleton); - if(InvadersGame.Singleton) + if (InvadersGame.Singleton) + { InvadersGame.Singleton.isGameOver.OnValueChanged += OnGameOver; + } } private void Update() { - if (!IsServer) return; transform.Translate(0, -m_TravelSpeed * Time.deltaTime, 0); - if (transform.position.y < k_YBoundary) Destroy(gameObject); + if (transform.position.y < k_YBoundary) + { + NetworkObject.Despawn(); + } } - public override void OnDestroy() + public override void OnNetworkDespawn() { - base.OnDestroy(); - if (InvadersGame.Singleton) InvadersGame.Singleton.isGameOver.OnValueChanged -= OnGameOver; + if (InvadersGame.Singleton) + { + InvadersGame.Singleton.isGameOver.OnValueChanged -= OnGameOver; + } } private void OnTriggerEnter2D(Collider2D collider) @@ -58,8 +66,8 @@ private void OnTriggerEnter2D(Collider2D collider) var hitPlayer = collider.gameObject.GetComponent(); if (hitPlayer != null) { - hitPlayer.HitByBullet(); NetworkObject.Despawn(); + hitPlayer.HitByBullet(); return; } @@ -82,8 +90,8 @@ void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation) private void OnGameOver(bool oldValue, bool newValue) { enabled = false; - + // On game over destroy the bullets - if (IsServer) NetworkObject.Despawn(); + NetworkObject.Despawn(); } } diff --git a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs index 734dd3266..021559c4e 100644 --- a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs +++ b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs @@ -27,8 +27,6 @@ public override void OnNetworkSpawn() private void Update() { - if (!IsServer) return; - transform.Translate(0, m_TravelSpeed * Time.deltaTime, 0); if (transform.position.y > k_YBoundary) From b29d3d1a7c8632c57df1bd6392068a8bdf45dea1 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Thu, 9 Mar 2023 15:17:17 -0500 Subject: [PATCH 3/3] formatted a file --- Basic/Invaders/Assets/Scripts/EnemyBullet.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs index ab07b8879..61059e3e8 100644 --- a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs +++ b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs @@ -12,7 +12,7 @@ public class EnemyBullet : NetworkBehaviour [SerializeField] [Tooltip("The constant speed at which the Bullet travels")] private float m_TravelSpeed = 3.0f; - + [SerializeField] ParticleSystem m_ShieldExplosionParticle; @@ -29,7 +29,7 @@ public override void OnNetworkSpawn() { return; } - + Assert.IsTrue(InvadersGame.Singleton); if (InvadersGame.Singleton) @@ -75,12 +75,12 @@ private void OnTriggerEnter2D(Collider2D collider) if (hitShield != null) { SpawnExplosionVFXClientRpc(transform.position, Quaternion.identity); - + Destroy(hitShield.gameObject); NetworkObject.Despawn(); } } - + [ClientRpc] void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation) { @@ -90,7 +90,7 @@ void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation) private void OnGameOver(bool oldValue, bool newValue) { enabled = false; - + // On game over destroy the bullets NetworkObject.Despawn(); }