diff --git a/Basic/Invaders/Assets/Prefabs/Invaders Game Manager.prefab b/Basic/Invaders/Assets/Prefabs/Invaders Game Manager.prefab index 3b9b380f..f09cf54f 100644 --- a/Basic/Invaders/Assets/Prefabs/Invaders Game Manager.prefab +++ b/Basic/Invaders/Assets/Prefabs/Invaders Game Manager.prefab @@ -9,8 +9,8 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 4266113290039088506} - - component: {fileID: 4266113290039088507} - component: {fileID: 2756959678467573149} + - component: {fileID: 4266113290039088507} m_Layer: 0 m_Name: Invaders Game Manager m_TagString: Untagged @@ -25,14 +25,35 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4266113290039088508} + serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2756959678467573149 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4266113290039088508} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 2783302607 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 --- !u!114 &4266113290039088507 MonoBehaviour: m_ObjectHideFlags: 0 @@ -45,11 +66,16 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d643b609c6a71c54984c1ad454faa28b, type: 3} m_Name: m_EditorClassIdentifier: - enemy1Prefab: {fileID: 100000, guid: 43ae6ad1bdadd0747811bb7a00740c2c, type: 3} - enemy2Prefab: {fileID: 100000, guid: 38a8441efb2d6d245adbf08f08705374, type: 3} - enemy3Prefab: {fileID: 100000, guid: aa50d36274d0dfc4faff2c69820b3711, type: 3} - superEnemyPrefab: {fileID: 100000, guid: e811e4fb062e31444aaf36dbec5de77c, type: 3} - shieldPrefab: {fileID: 100000, guid: b990a6e6dcf54b646a7397028fed6045, type: 3} + enemy1Prefab: {fileID: 2037077820506562951, guid: 43ae6ad1bdadd0747811bb7a00740c2c, + type: 3} + enemy2Prefab: {fileID: 1361474663871475650, guid: 38a8441efb2d6d245adbf08f08705374, + type: 3} + enemy3Prefab: {fileID: 7651551376209856180, guid: aa50d36274d0dfc4faff2c69820b3711, + type: 3} + superEnemyPrefab: {fileID: 3955316467729786863, guid: e811e4fb062e31444aaf36dbec5de77c, + type: 3} + shieldPrefab: {fileID: 6564576217164106930, guid: b990a6e6dcf54b646a7397028fed6045, + type: 3} gameTimerText: {fileID: 0} scoreText: {fileID: 0} livesText: {fileID: 0} @@ -61,19 +87,3 @@ MonoBehaviour: m_EnemyMovingDirection: m_InternalValue: 0.3 m_RandomThresholdForSaucerCreation: 0.92 ---- !u!114 &2756959678467573149 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 4266113290039088508} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 951099334 - AlwaysReplicateAsRoot: 0 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 diff --git a/Basic/Invaders/Assets/Prefabs/enemy1.prefab b/Basic/Invaders/Assets/Prefabs/enemy1.prefab index 47607024..ec836fb7 100644 --- a/Basic/Invaders/Assets/Prefabs/enemy1.prefab +++ b/Basic/Invaders/Assets/Prefabs/enemy1.prefab @@ -10,11 +10,11 @@ GameObject: m_Component: - component: {fileID: 400000} - component: {fileID: 21200000} - - component: {fileID: 2037077820506562951} - component: {fileID: 11400000} - component: {fileID: 6100000} - component: {fileID: 5000000} - component: {fileID: 4223722966607089368} + - component: {fileID: 2037077820506562951} m_Layer: 0 m_Name: enemy1 m_TagString: Untagged @@ -89,27 +89,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &2037077820506562951 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 416377817 - InScenePlacedSourceGlobalObjectIdHash: 0 - AlwaysReplicateAsRoot: 0 - SynchronizeTransform: 1 - ActiveSceneSynchronization: 0 - SceneMigrationSynchronization: 1 - SpawnWithObservers: 1 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 @@ -123,7 +102,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: score: 10 - bulletPrefab: {fileID: 100000, guid: ac6ffb0923d0f7448852a32312a0263c, type: 3} + bulletPrefab: {fileID: 2336033219118315817, guid: ac6ffb0923d0f7448852a32312a0263c, + type: 3} GraceShootingPeriod: 1 --- !u!61 &6100000 BoxCollider2D: @@ -228,3 +208,24 @@ MonoBehaviour: InLocalSpace: 0 Interpolate: 0 SlerpPosition: 0 +--- !u!114 &2037077820506562951 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 100000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 416377817 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 diff --git a/Basic/Invaders/Assets/Prefabs/enemy2.prefab b/Basic/Invaders/Assets/Prefabs/enemy2.prefab index 0c405858..0116df89 100644 --- a/Basic/Invaders/Assets/Prefabs/enemy2.prefab +++ b/Basic/Invaders/Assets/Prefabs/enemy2.prefab @@ -89,27 +89,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &1361474663871475650 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 3854485402 - InScenePlacedSourceGlobalObjectIdHash: 0 - AlwaysReplicateAsRoot: 0 - SynchronizeTransform: 1 - ActiveSceneSynchronization: 0 - SceneMigrationSynchronization: 1 - SpawnWithObservers: 1 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 @@ -123,7 +102,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: score: 20 - bulletPrefab: {fileID: 100000, guid: ac6ffb0923d0f7448852a32312a0263c, type: 3} + bulletPrefab: {fileID: 2336033219118315817, guid: ac6ffb0923d0f7448852a32312a0263c, + type: 3} GraceShootingPeriod: 1 --- !u!61 &6100000 BoxCollider2D: @@ -228,3 +208,24 @@ MonoBehaviour: InLocalSpace: 0 Interpolate: 0 SlerpPosition: 0 +--- !u!114 &1361474663871475650 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 100000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 3854485402 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 diff --git a/Basic/Invaders/Assets/Prefabs/enemy3.prefab b/Basic/Invaders/Assets/Prefabs/enemy3.prefab index d313ec52..fed2aca6 100644 --- a/Basic/Invaders/Assets/Prefabs/enemy3.prefab +++ b/Basic/Invaders/Assets/Prefabs/enemy3.prefab @@ -89,27 +89,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &7651551376209856180 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 3432298371 - InScenePlacedSourceGlobalObjectIdHash: 0 - AlwaysReplicateAsRoot: 0 - SynchronizeTransform: 1 - ActiveSceneSynchronization: 0 - SceneMigrationSynchronization: 1 - SpawnWithObservers: 1 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 @@ -123,7 +102,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: score: 30 - bulletPrefab: {fileID: 100000, guid: ac6ffb0923d0f7448852a32312a0263c, type: 3} + bulletPrefab: {fileID: 2336033219118315817, guid: ac6ffb0923d0f7448852a32312a0263c, + type: 3} GraceShootingPeriod: 1 --- !u!61 &6100000 BoxCollider2D: @@ -228,3 +208,24 @@ MonoBehaviour: InLocalSpace: 0 Interpolate: 0 SlerpPosition: 0 +--- !u!114 &7651551376209856180 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 100000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 3432298371 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 diff --git a/Basic/Invaders/Assets/Prefabs/player.prefab b/Basic/Invaders/Assets/Prefabs/player.prefab index 7eec7f0c..90456e79 100644 --- a/Basic/Invaders/Assets/Prefabs/player.prefab +++ b/Basic/Invaders/Assets/Prefabs/player.prefab @@ -10,10 +10,10 @@ GameObject: m_Component: - component: {fileID: 400000} - component: {fileID: 21200000} - - component: {fileID: 5053919295125167455} - component: {fileID: 11400000} - component: {fileID: 6100000} - component: {fileID: 5000000} + - component: {fileID: 5053919295125167455} - component: {fileID: 2800302518702725854} m_Layer: 0 m_Name: player @@ -89,26 +89,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &5053919295125167455 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 915374853 - AlwaysReplicateAsRoot: 0 - SynchronizeTransform: 1 - ActiveSceneSynchronization: 0 - SceneMigrationSynchronization: 1 - SpawnWithObservers: 1 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 @@ -121,7 +101,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 74fc8e136514c41458e083092b30afed, type: 3} m_Name: m_EditorClassIdentifier: - bulletPrefab: {fileID: 100000, guid: 6dfb311828493c3409e0f7973872052e, type: 3} + bulletPrefab: {fileID: 2859882261451710602, guid: 6dfb311828493c3409e0f7973872052e, + type: 3} m_MoveSpeed: 4.5 m_Lives: m_InternalValue: 3 @@ -203,6 +184,27 @@ Rigidbody2D: m_SleepingMode: 1 m_CollisionDetection: 0 m_Constraints: 0 +--- !u!114 &5053919295125167455 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 100000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 915374853 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 --- !u!114 &2800302518702725854 MonoBehaviour: m_ObjectHideFlags: 0 @@ -215,6 +217,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: df2868252ab5c4d1da357e8f11f1b524, type: 3} m_Name: m_EditorClassIdentifier: + UseUnreliableDeltas: 0 SyncPositionX: 1 SyncPositionY: 0 SyncPositionZ: 0 diff --git a/Basic/Invaders/Assets/Prefabs/superEnemy.prefab b/Basic/Invaders/Assets/Prefabs/superEnemy.prefab index ac87fa81..0e206d8d 100644 --- a/Basic/Invaders/Assets/Prefabs/superEnemy.prefab +++ b/Basic/Invaders/Assets/Prefabs/superEnemy.prefab @@ -11,11 +11,11 @@ GameObject: - component: {fileID: 400000} - component: {fileID: 21200000} - component: {fileID: 5000000} - - component: {fileID: 3955316467729786863} - component: {fileID: 11400006} - component: {fileID: 11400004} - component: {fileID: 6100000} - component: {fileID: 8984612552865464625} + - component: {fileID: 3955316467729786863} m_Layer: 0 m_Name: superEnemy m_TagString: Untagged @@ -117,26 +117,6 @@ Rigidbody2D: m_SleepingMode: 1 m_CollisionDetection: 0 m_Constraints: 0 ---- !u!114 &3955316467729786863 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} - m_Name: - m_EditorClassIdentifier: - GlobalObjectIdHash: 1887726101 - AlwaysReplicateAsRoot: 0 - SynchronizeTransform: 1 - ActiveSceneSynchronization: 0 - SceneMigrationSynchronization: 1 - SpawnWithObservers: 1 - DontDestroyWithOwner: 0 - AutoObjectParentSync: 1 --- !u!114 &11400006 MonoBehaviour: m_ObjectHideFlags: 0 @@ -150,7 +130,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: score: 75 - bulletPrefab: {fileID: 100000, guid: ac6ffb0923d0f7448852a32312a0263c, type: 3} + bulletPrefab: {fileID: 2336033219118315817, guid: ac6ffb0923d0f7448852a32312a0263c, + type: 3} GraceShootingPeriod: 1 --- !u!114 &11400004 MonoBehaviour: @@ -222,6 +203,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} m_Name: m_EditorClassIdentifier: + UseUnreliableDeltas: 0 SyncPositionX: 1 SyncPositionY: 0 SyncPositionZ: 0 @@ -240,3 +222,24 @@ MonoBehaviour: InLocalSpace: 0 Interpolate: 0 SlerpPosition: 0 +--- !u!114 &3955316467729786863 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 100000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} + m_Name: + m_EditorClassIdentifier: + GlobalObjectIdHash: 1887726101 + InScenePlacedSourceGlobalObjectIdHash: 0 + AlwaysReplicateAsRoot: 0 + SynchronizeTransform: 1 + ActiveSceneSynchronization: 0 + SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 + DontDestroyWithOwner: 0 + AutoObjectParentSync: 1 diff --git a/Basic/Invaders/Assets/Scripts/EnemyAgent.cs b/Basic/Invaders/Assets/Scripts/EnemyAgent.cs index 2cb398f5..2993ba95 100644 --- a/Basic/Invaders/Assets/Scripts/EnemyAgent.cs +++ b/Basic/Invaders/Assets/Scripts/EnemyAgent.cs @@ -1,4 +1,5 @@ using System; +using Unity.Mathematics; using Unity.Netcode; using UnityEngine; using UnityEngine.Assertions; @@ -10,7 +11,7 @@ public class EnemyAgent : NetworkBehaviour private const float k_ShootTimer = 1.25f; [Header("Enemy Settings")] public int score = 50; - public GameObject bulletPrefab; + public NetworkObject bulletPrefab; public float GraceShootingPeriod = 1.0f; // A period of time in which the enemy will not shoot at the start public bool canShoot { get; set; } @@ -80,8 +81,9 @@ private void Update() private void SpawnBullet() { - var myBullet = Instantiate(bulletPrefab, transform.position - Vector3.up, Quaternion.identity); - myBullet.GetComponent().Spawn(); + bulletPrefab.InstantiateAndSpawn(NetworkManager.Singleton, + position: transform.position - Vector3.up, + rotation: quaternion.identity); } private void OnTriggerEnter2D(Collider2D collider) diff --git a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs index 67269802..5bbf2513 100644 --- a/Basic/Invaders/Assets/Scripts/EnemyBullet.cs +++ b/Basic/Invaders/Assets/Scripts/EnemyBullet.cs @@ -74,15 +74,15 @@ private void OnTriggerEnter2D(Collider2D collider) var hitShield = collider.gameObject.GetComponent(); if (hitShield != null) { - SpawnExplosionVFXClientRpc(transform.position, Quaternion.identity); + ClientSpawnExplosionVFXRpc(transform.position, Quaternion.identity); Destroy(hitShield.gameObject); NetworkObject.Despawn(); } } - [ClientRpc] - void SpawnExplosionVFXClientRpc(Vector3 spawnPosition, Quaternion spawnRotation) + [Rpc(SendTo.ClientsAndHost)] + void ClientSpawnExplosionVFXRpc(Vector3 spawnPosition, Quaternion spawnRotation) { Instantiate(m_ShieldExplosionParticle, spawnPosition, spawnRotation); } diff --git a/Basic/Invaders/Assets/Scripts/InvadersGame.cs b/Basic/Invaders/Assets/Scripts/InvadersGame.cs index 1f4113f4..c94635e5 100644 --- a/Basic/Invaders/Assets/Scripts/InvadersGame.cs +++ b/Basic/Invaders/Assets/Scripts/InvadersGame.cs @@ -39,11 +39,11 @@ public class InvadersGame : NetworkBehaviour private const float k_BottomBoundaryOffset = 1.25f; [Header("Prefab settings")] - public GameObject enemy1Prefab; - public GameObject enemy2Prefab; - public GameObject enemy3Prefab; - public GameObject superEnemyPrefab; - public GameObject shieldPrefab; + public NetworkObject enemy1Prefab; + public NetworkObject enemy2Prefab; + public NetworkObject enemy3Prefab; + public NetworkObject superEnemyPrefab; + public NetworkObject shieldPrefab; [Header("UI Settings")] public TMP_Text gameTimerText; @@ -82,7 +82,7 @@ public class InvadersGame : NetworkBehaviour // the timer should only be synced at the beginning // and then let the client update it in a predictive manner private bool m_ReplicatedTimeSent = false; - private GameObject m_Saucer; + private NetworkObject m_Saucer; private List m_Shields = new List(); private float m_TimeRemaining; @@ -146,9 +146,9 @@ public override void OnNetworkDespawn() { m_Enemies.Clear(); m_Shields.Clear(); - } - NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected; + NetworkManager.Singleton.OnConnectionEvent -= ServerOnConnectionEvent; + } } internal static event Action OnSingletonReady; @@ -187,18 +187,21 @@ public override void OnNetworkSpawn() if (IsServer) { - NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected; + NetworkManager.Singleton.OnConnectionEvent += ServerOnConnectionEvent; } base.OnNetworkSpawn(); } - private void OnClientConnected(ulong clientId) + private void ServerOnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) { + if (connectionEventData.EventType != ConnectionEvent.ClientConnected) + return; + if (m_ReplicatedTimeSent) { // Send the RPC only to the newly connected client - SetReplicatedTimeRemainingClientRPC(m_TimeRemaining, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new List() { clientId } } }); + ClientSetReplicatedTimeRemainingRpc(m_TimeRemaining, RpcTarget.Single(connectionEventData.ClientId, RpcTargetUse.Temp)); } } @@ -218,7 +221,7 @@ private bool ShouldStartCountDown() //While we are counting down, continually set the replicated time remaining value for clients (client should only receive the update once) if (m_CountdownStarted.Value && !m_ReplicatedTimeSent) { - SetReplicatedTimeRemainingClientRPC(m_DelayedStartTime); + ClientSetReplicatedTimeRemainingRpc(m_DelayedStartTime, RpcTarget.ClientsAndHost); m_ReplicatedTimeSent = true; } @@ -233,9 +236,9 @@ private bool ShouldStartCountDown() /// will deal with updating it. For that, we use a ClientRPC /// /// - /// - [ClientRpc] - private void SetReplicatedTimeRemainingClientRPC(float delayedStartTime, ClientRpcParams clientRpcParams = new ClientRpcParams()) + /// + [Rpc(SendTo.SpecifiedInParams)] + private void ClientSetReplicatedTimeRemainingRpc(float delayedStartTime, RpcParams rpcParams) { // See the ShouldStartCountDown method for when the server updates the value if (m_TimeRemaining == 0) @@ -432,7 +435,7 @@ public void SetGameEnd(GameOverReason reason) if (reason != GameOverReason.Death) { this.isGameOver.Value = true; - BroadcastGameOverClientRpc(reason); // Notify our clients! + ClientBroadcastGameOverRpc(reason); // Notify our clients! return; } @@ -449,8 +452,8 @@ public void SetGameEnd(GameOverReason reason) this.isGameOver.Value = true; } - [ClientRpc] - public void BroadcastGameOverClientRpc(GameOverReason reason) + [Rpc(SendTo.ClientsAndHost)] + public void ClientBroadcastGameOverRpc(GameOverReason reason) { var localPlayerObject = NetworkManager.Singleton.LocalClient.PlayerObject; Assert.IsNotNull(localPlayerObject); @@ -528,7 +531,7 @@ public void ExitGame() SceneTransitionHandler.sceneTransitionHandler.ExitAndLoadStartMenu(); } - private void CreateShield(GameObject prefab, int posX, int posY) + private void CreateShield(NetworkObject prefab, int posX, int posY) { Assert.IsTrue(IsServer, "Create Shield should be called server-side only!"); @@ -547,10 +550,10 @@ private void CreateShield(GameObject prefab, int posX, int posY) continue; } - var shield = Instantiate(prefab, new Vector3(x - 1, y, 0), Quaternion.identity); - // Spawn the Networked Object, this should notify the clients - shield.GetComponent().Spawn(); + prefab.InstantiateAndSpawn(NetworkManager.Singleton, + position: new Vector3(x - 1, y, 0), + rotation: Quaternion.identity); xcount += 1; } @@ -570,22 +573,21 @@ private void CreateSuperEnemy() { Assert.IsTrue(IsServer, "Create Saucer should be called server-side only!"); - m_Saucer = Instantiate(superEnemyPrefab, saucerSpawnPoint.position, Quaternion.identity); - // Spawn the Networked Object, this should notify the clients - m_Saucer.GetComponent().Spawn(); + m_Saucer = superEnemyPrefab.InstantiateAndSpawn(NetworkManager.Singleton, + position: saucerSpawnPoint.position, + rotation: Quaternion.identity); } - private void CreateEnemy(GameObject prefab, float posX, float posY) + private void CreateEnemy(NetworkObject prefab, float posX, float posY) { Assert.IsTrue(IsServer, "Create Enemy should be called server-side only!"); - var enemy = Instantiate(prefab); - enemy.transform.position = new Vector3(posX, posY, 0.0f); - enemy.GetComponent().Setup(Mathf.RoundToInt(posX), Mathf.RoundToInt(posY)); - // Spawn the Networked Object, this should notify the clients - enemy.GetComponent().Spawn(); + var enemy = prefab.InstantiateAndSpawn(NetworkManager.Singleton, + position: new Vector3(posX, posY, 0.0f), + rotation: Quaternion.identity); + enemy.GetComponent().Setup(Mathf.RoundToInt(posX), Mathf.RoundToInt(posY)); } public void CreateEnemies() diff --git a/Basic/Invaders/Assets/Scripts/LobbyControl.cs b/Basic/Invaders/Assets/Scripts/LobbyControl.cs index ebcfae2c..59b20137 100644 --- a/Basic/Invaders/Assets/Scripts/LobbyControl.cs +++ b/Basic/Invaders/Assets/Scripts/LobbyControl.cs @@ -34,7 +34,7 @@ public override void OnNetworkSpawn() m_AllPlayersInLobby = false; //Server will be notified when a client connects - NetworkManager.OnClientConnectedCallback += OnClientConnectedCallback; + NetworkManager.Singleton.OnConnectionEvent += ServerOnConnectionEvent; SceneTransitionHandler.sceneTransitionHandler.OnClientLoadedScene += ClientLoadedScene; } @@ -77,11 +77,12 @@ private void UpdateAndCheckPlayersInLobby() foreach (var clientLobbyStatus in m_ClientsInLobby) { - SendClientReadyStatusUpdatesClientRpc(clientLobbyStatus.Key, clientLobbyStatus.Value); + ClientSendReadyStatusUpdatesRpc(clientLobbyStatus.Key, clientLobbyStatus.Value); if (!NetworkManager.Singleton.ConnectedClients.ContainsKey(clientLobbyStatus.Key)) - + { //If some clients are still loading into the lobby scene then this is false m_AllPlayersInLobby = false; + } } CheckForAllPlayersReady(); @@ -111,27 +112,30 @@ private void ClientLoadedScene(ulong clientId) /// Since we are entering a lobby and Netcode's NetworkManager is spawning the player, /// the server can be configured to only listen for connected clients at this stage. /// - /// client that connected - private void OnClientConnectedCallback(ulong clientId) + /// + /// Connection event to check for which player id is connecting. + private void ServerOnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) { - if (IsServer) - { - if (!m_ClientsInLobby.ContainsKey(clientId)) m_ClientsInLobby.Add(clientId, false); - GenerateUserStatsForLobby(); + if (connectionEventData.EventType != ConnectionEvent.ClientConnected) + return; - UpdateAndCheckPlayersInLobby(); - } + var clientId = connectionEventData.ClientId; + if (!m_ClientsInLobby.ContainsKey(clientId)) + m_ClientsInLobby.Add(clientId, false); + + GenerateUserStatsForLobby(); + UpdateAndCheckPlayersInLobby(); } /// - /// SendClientReadyStatusUpdatesClientRpc + /// ClientSendReadyStatusUpdatesRpc /// Sent from the server to the client when a player's status is updated. /// This also populates the connected clients' (excluding host) player state in the lobby /// /// /// - [ClientRpc] - private void SendClientReadyStatusUpdatesClientRpc(ulong clientId, bool isReady) + [Rpc(SendTo.ClientsAndHost)] + private void ClientSendReadyStatusUpdatesRpc(ulong clientId, bool isReady) { if (!IsServer) { @@ -162,7 +166,7 @@ private void CheckForAllPlayersReady() if (allPlayersAreReady) { //Remove our client connected callback - NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback; + NetworkManager.Singleton.OnConnectionEvent -= ServerOnConnectionEvent; //Remove our scene loaded callback SceneTransitionHandler.sceneTransitionHandler.OnClientLoadedScene -= ClientLoadedScene; @@ -186,7 +190,7 @@ public void PlayerIsReady() } else { - OnClientIsReadyServerRpc(NetworkManager.Singleton.LocalClientId); + ServerOnClientIsReadyRpc(NetworkManager.Singleton.LocalClientId); } GenerateUserStatsForLobby(); @@ -197,8 +201,8 @@ public void PlayerIsReady() /// Sent to the server when the player clicks the ready button /// /// clientId that is ready - [ServerRpc(RequireOwnership = false)] - private void OnClientIsReadyServerRpc(ulong clientid) + [Rpc(SendTo.Server)] + private void ServerOnClientIsReadyRpc(ulong clientid) { if (m_ClientsInLobby.ContainsKey(clientid)) { diff --git a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs index 511fd833..d6a3d4c4 100644 --- a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs +++ b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs @@ -50,7 +50,7 @@ private void OnTriggerEnter2D(Collider2D collider) // Only the server can despawn a NetworkObject hitEnemy.NetworkObject.Despawn(); - SpawnExplosionVFXClientRPC(transform.position, Quaternion.identity); + ClientSpawnExplosionVFXRpc(transform.position, Quaternion.identity); NetworkObject.Despawn(); @@ -65,10 +65,10 @@ private void OnTriggerEnter2D(Collider2D collider) } } - [ClientRpc] - void SpawnExplosionVFXClientRPC(Vector3 spawnPosition, Quaternion spawnRotation) + [Rpc(SendTo.ClientsAndHost)] + void ClientSpawnExplosionVFXRpc(Vector3 spawnPosition, Quaternion spawnRotation) { - // this instantiates at the position of the bullet, there is an offset in the Y axis on the + // 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); } diff --git a/Basic/Invaders/Assets/Scripts/PlayerControl.cs b/Basic/Invaders/Assets/Scripts/PlayerControl.cs index 459a907c..7f60460c 100644 --- a/Basic/Invaders/Assets/Scripts/PlayerControl.cs +++ b/Basic/Invaders/Assets/Scripts/PlayerControl.cs @@ -3,12 +3,11 @@ using Unity.Netcode; using UnityEngine; using UnityEngine.Assertions; -using UnityEngine.Rendering.VirtualTexturing; public class PlayerControl : NetworkBehaviour { [Header("Weapon Settings")] - public GameObject bulletPrefab; + public NetworkObject bulletPrefab; [Header("Movement Settings")] [SerializeField] @@ -32,8 +31,7 @@ public class PlayerControl : NetworkBehaviour private NetworkVariable m_MoveX = new NetworkVariable(0); - private GameObject m_MyBullet; - private ClientRpcParams m_OwnerRPCParams; + private NetworkObject m_MyBullet; [SerializeField] private SpriteRenderer m_PlayerVisual; @@ -114,7 +112,6 @@ public override void OnNetworkSpawn() m_Lives.OnValueChanged += OnLivesChanged; m_Score.OnValueChanged += OnScoreChanged; - if (IsServer) m_OwnerRPCParams = new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new[] { OwnerClientId } } }; if (!InvadersGame.Singleton) InvadersGame.OnSingletonReady += SubscribeToDelegatesAndUpdateValues; @@ -189,20 +186,21 @@ private void InGameUpdate() transform.position + newMovement, m_MoveSpeed * Time.deltaTime); } - if (Input.GetKeyDown(KeyCode.Space)) ShootServerRPC(); + if (Input.GetKeyDown(KeyCode.Space)) ServerShootRpc(); } - [ServerRpc] - private void ShootServerRPC() + [Rpc(SendTo.Server)] + private void ServerShootRpc() { if (!m_IsAlive) return; if (m_MyBullet == null) { - m_MyBullet = Instantiate(bulletPrefab, transform.position + Vector3.up, Quaternion.identity); + m_MyBullet = bulletPrefab.InstantiateAndSpawn(NetworkManager.Singleton, + position: transform.position + Vector3.up, + rotation: Quaternion.identity); m_MyBullet.GetComponent().owner = this; - m_MyBullet.GetComponent().Spawn(); } } @@ -220,7 +218,7 @@ public void HitByBullet() m_MoveX.Value = 0; m_Lives.Value = 0; InvadersGame.Singleton.SetGameEnd(GameOverReason.Death); - NotifyGameOverClientRpc(GameOverReason.Death, m_OwnerRPCParams); + ClientNotifyGameOverRpc(GameOverReason.Death); Instantiate(m_ExplosionParticleSystem, transform.position, quaternion.identity); // Hide graphics of this player object server-side. Note we don't want to destroy the object as it @@ -233,8 +231,8 @@ public void HitByBullet() } } - [ClientRpc] - private void NotifyGameOverClientRpc(GameOverReason reason, ClientRpcParams clientParams) + [Rpc(SendTo.Owner)] + private void ClientNotifyGameOverRpc(GameOverReason reason) { NotifyGameOver(reason); } diff --git a/Basic/Invaders/Packages/manifest.json b/Basic/Invaders/Packages/manifest.json index fdb558c3..4dee139b 100644 --- a/Basic/Invaders/Packages/manifest.json +++ b/Basic/Invaders/Packages/manifest.json @@ -7,7 +7,7 @@ "com.unity.ide.vscode": "1.2.5", "com.unity.multiplayer.samples.coop": "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#v2.4.0", "com.unity.multiplayer.tools": "1.1.1", - "com.unity.netcode.gameobjects": "1.7.1", + "com.unity.netcode.gameobjects": "1.8.1", "com.unity.render-pipelines.universal": "14.0.11", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.7.6", diff --git a/Basic/Invaders/Packages/packages-lock.json b/Basic/Invaders/Packages/packages-lock.json index 21a7e868..7485e7b0 100644 --- a/Basic/Invaders/Packages/packages-lock.json +++ b/Basic/Invaders/Packages/packages-lock.json @@ -117,7 +117,7 @@ "url": "https://packages.unity.com" }, "com.unity.netcode.gameobjects": { - "version": "1.7.1", + "version": "1.8.1", "depth": 0, "source": "registry", "dependencies": { diff --git a/Basic/Invaders/README.md b/Basic/Invaders/README.md index 7d9a8399..479af632 100644 --- a/Basic/Invaders/README.md +++ b/Basic/Invaders/README.md @@ -4,7 +4,7 @@ # Invaders [![UnityVersion](https://img.shields.io/badge/Unity%20Version:-2022.3%20LTS-57b9d3.svg?logo=unity&color=2196F3)](https://unity.com/releases/editor/whats-new/2022.3.0) -[![NetcodeVersion](https://img.shields.io/badge/Netcode%20Version:-1.7.1-57b9d3.svg?logo=unity&color=2196F3)](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.7.1) +[![NetcodeVersion](https://img.shields.io/badge/Netcode%20Version:-1.8.1-57b9d3.svg?logo=unity&color=2196F3)](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.8.1) [![LatestRelease](https://img.shields.io/badge/Latest%20%20Github%20Release:-v1.5.0-57b9d3.svg?logo=github&color=brightgreen)](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize/releases/tag/v1.5.0)

diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e98f40a..c5667531 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,10 @@ #### Changed - Upgraded to IDE Rider v3.0.28 (#166) - Upgraded to Unity 2022.3.27f1 (#169) +- Upgraded to Netcode for GameObjects v1.8.1 (#172) + - Upgraded to the newer API for Rpcs, Universal Rpcs + - Upgraded to the newer API for NetworkObject spawning to use NetworkObject.InstantiateAndSpawn + - Upgraded usage of NetworkManager.OnClientConnectedCallback to the new NetworkManager.OnConnectionEvent #### Fixed - Optimized NetworkTransform on all networked prefabs so the Clients objects movements are closer to the Host ones (#168)