From 3c21407c2c3cf275dcda1aa3b0b5d8a9d557be46 Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Fri, 20 Dec 2024 23:52:39 +0500 Subject: [PATCH] Fixed the ability not syncing issue --- .../Action/Abilities/SlowZoneAbility.asset | 2 +- Assets/GameData/NetworkPrefabs.asset | 5 ++ .../Abilities/SlowDownZonePrefab.prefab | 3 +- Assets/Scripts/Gameplay/Ability.cs | 14 +++-- Assets/Scripts/Gameplay/AbilitySystem.cs | 60 +++++++++++++------ Assets/Scripts/Gameplay/SlowZoneAbility.cs | 38 +----------- Assets/Scripts/Gameplay/SlowZonePrefab.cs | 19 +++++- 7 files changed, 79 insertions(+), 62 deletions(-) diff --git a/Assets/GameData/Action/Abilities/SlowZoneAbility.asset b/Assets/GameData/Action/Abilities/SlowZoneAbility.asset index 457cb7d..07dda9e 100644 --- a/Assets/GameData/Action/Abilities/SlowZoneAbility.asset +++ b/Assets/GameData/Action/Abilities/SlowZoneAbility.asset @@ -19,4 +19,4 @@ MonoBehaviour: abilityRadius: 4 abilityDuration: 3 abilityMagnitude: 0.3 - slowZonePrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} + abilityPrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} diff --git a/Assets/GameData/NetworkPrefabs.asset b/Assets/GameData/NetworkPrefabs.asset index 88817e4..64eddc2 100644 --- a/Assets/GameData/NetworkPrefabs.asset +++ b/Assets/GameData/NetworkPrefabs.asset @@ -69,3 +69,8 @@ MonoBehaviour: SourcePrefabToOverride: {fileID: 0} SourceHashToOverride: 0 OverridingTargetPrefab: {fileID: 0} + - Override: 0 + Prefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} + SourcePrefabToOverride: {fileID: 0} + SourceHashToOverride: 0 + OverridingTargetPrefab: {fileID: 0} diff --git a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab index 0f1ed40..46a5fc0 100644 --- a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab +++ b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab @@ -118,5 +118,4 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 16c014692584a9a438b902616cc2dd35, type: 3} m_Name: m_EditorClassIdentifier: - zoneRadius: 5 - slowDuration: 3 + Ability: {fileID: 11400000, guid: e4794e2f71f66a74486c797344695ce7, type: 2} diff --git a/Assets/Scripts/Gameplay/Ability.cs b/Assets/Scripts/Gameplay/Ability.cs index 899d942..ec10e7a 100644 --- a/Assets/Scripts/Gameplay/Ability.cs +++ b/Assets/Scripts/Gameplay/Ability.cs @@ -10,13 +10,19 @@ public abstract class Ability : ScriptableObject public float abilityRadius; public float abilityDuration; public float abilityMagnitude; + + [SerializeField] + private GameObject abilityPrefab; // Prefab associated with this ability + + public GameObject GetPrefab() + { + return abilityPrefab; + } + public void ActivateAbility(GameObject owner) { Activate(owner); } protected abstract void Activate(GameObject owner); // Logic for the specific ability - - - public abstract void OnDrawAbilityGizmos(Vector3 pos); -} +} \ No newline at end of file diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index 5509277..12d8c7b 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -1,8 +1,9 @@ using System.Collections; using System.Collections.Generic; +using Unity.Netcode; using UnityEngine; -public class AbilitySystem : MonoBehaviour +public class AbilitySystem : NetworkBehaviour { [Header("Assigned Abilities")] public List abilities = new List(); @@ -12,6 +13,7 @@ public class AbilitySystem : MonoBehaviour private HashSet abilitiesOnCooldown = new HashSet(); [SerializeField] private GameObject currentAbilityIndicator; + void Update() { if (isAbilityActive) @@ -52,7 +54,7 @@ public class AbilitySystem : MonoBehaviour { return isAbilityActive; // Returns true if an ability mode is currently active } - + private void ToggleAbilityMode(Ability ability) { if (isAbilityActive && activeAbility == ability) @@ -85,7 +87,8 @@ public class AbilitySystem : MonoBehaviour { if (activeAbility != null) { - activeAbility.ActivateAbility(gameObject); + var spawnPosition = currentAbilityIndicator.transform.position; + SpawnAbilityServerRpc(activeAbility.abilityKey, spawnPosition); StartCoroutine(StartCooldown(activeAbility)); DeactivateAbilityMode(); } @@ -105,7 +108,40 @@ public class AbilitySystem : MonoBehaviour Debug.Log($"{ability.abilityName} is off cooldown."); } - + /// + /// Spawns the requested ability on the server. + /// + [ServerRpc(RequireOwnership = false)] + private void SpawnAbilityServerRpc(string abilityKey, Vector3 position, ServerRpcParams rpcParams = default) + { + var ability = abilities.Find(a => a.abilityKey == abilityKey); + if (ability == null) + { + Debug.LogError($"Ability {abilityKey} not found in the Ability System!"); + return; + } + + var prefab = ability.GetPrefab(); // Ensure your Ability class has a method to get the prefab + if (prefab == null) + { + Debug.LogError($"Prefab for Ability {abilityKey} is not assigned!"); + return; + } + + GameObject abilityInstance = Instantiate(prefab, position, Quaternion.identity); + var networkObject = abilityInstance.GetComponent(); + + if (networkObject != null) + { + networkObject.Spawn(); + Debug.Log($"Spawned {abilityKey} at {position}."); + } + else + { + Debug.LogError($"Ability prefab {abilityKey} must have a NetworkObject component."); + } + } + private void UpdateIndicatorPosition() { // Raycast to get the position of the cursor on the ground @@ -113,21 +149,7 @@ public class AbilitySystem : MonoBehaviour if (Physics.Raycast(ray, out RaycastHit hit)) { currentAbilityIndicator.transform.position = hit.point; - currentAbilityIndicator.transform.localScale = Vector3.one * activeAbility.abilityRadius; // Replace cooldownTime with your ability range if needed - + currentAbilityIndicator.transform.localScale = Vector3.one * activeAbility.abilityRadius; } } - - // private void OnDrawGizmos() - // { - // if (isAbilityActive && activeAbility is SlowZoneAbility slowZoneAbility) - // { - // // Draw the gizmo at the mouse position - // Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); - // if (Physics.Raycast(ray, out RaycastHit hit)) - // { - // slowZoneAbility.OnDrawAbilityGizmos(hit.point); - // } - // } - // } } diff --git a/Assets/Scripts/Gameplay/SlowZoneAbility.cs b/Assets/Scripts/Gameplay/SlowZoneAbility.cs index 8f1c6a7..2dbf40e 100644 --- a/Assets/Scripts/Gameplay/SlowZoneAbility.cs +++ b/Assets/Scripts/Gameplay/SlowZoneAbility.cs @@ -1,43 +1,11 @@ using UnityEngine; -using Unity.Netcode; [CreateAssetMenu(menuName = "Abilities/Slow Zone Ability")] public class SlowZoneAbility : Ability { - [Header("Slow Zone Settings")] - public GameObject slowZonePrefab; - protected override void Activate(GameObject owner) { - Camera mainCamera = Camera.main; - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - - if (Physics.Raycast(ray, out RaycastHit hit)) - { - Vector3 spawnPosition = hit.point; - - if (owner.TryGetComponent(out NetworkObject networkOwner)) - { - SpawnSlowZoneServerRpc(spawnPosition); - } - } - } - - - - [Rpc(SendTo.Server)] - private void SpawnSlowZoneServerRpc(Vector3 position) - { - GameObject slowZone = Instantiate(slowZonePrefab, position, Quaternion.identity); - slowZone.GetComponent().Spawn(); - slowZone.GetComponent().Ability = this; - Debug.Log($"Slow Zone spawned at {position}."); - } - - - public override void OnDrawAbilityGizmos(Vector3 pos) - { - Gizmos.color = new Color(0f, 0.5f, 1f, 0.2f); // Light blue with transparency - Gizmos.DrawSphere(pos, abilityRadius); + // No direct logic here, spawning is handled in AbilitySystem + Debug.Log($"SlowZoneAbility activated by {owner.name}."); } -} +} \ No newline at end of file diff --git a/Assets/Scripts/Gameplay/SlowZonePrefab.cs b/Assets/Scripts/Gameplay/SlowZonePrefab.cs index 528c1b2..4f57f8b 100644 --- a/Assets/Scripts/Gameplay/SlowZonePrefab.cs +++ b/Assets/Scripts/Gameplay/SlowZonePrefab.cs @@ -24,7 +24,7 @@ public class SlowZonePrefab : NetworkBehaviour yield return new WaitForSeconds(Ability.abilityDuration); // Destroy the zone after applying the slow effect - Destroy(gameObject); + DespawnZone(); } private void ApplySlowToPlayers() @@ -60,6 +60,23 @@ public class SlowZonePrefab : NetworkBehaviour Destroy(gameObject); } + private void DespawnZone() + { + if (IsServer) + { + var networkObject = GetComponent(); + if (networkObject != null) + { + networkObject.Despawn(true); // Despawn and destroy on the server + Debug.Log("SlowZonePrefab has been despawned and destroyed."); + } + else + { + Debug.LogError("SlowZonePrefab is missing a NetworkObject component!"); + } + } + } + // Debug visualization for the slow zone in the editor private void OnDrawGizmos() {