Fixed the ability not syncing issue

main
Hazim Bin Ijaz 4 weeks ago
parent bf8350d7be
commit 3c21407c2c

@ -19,4 +19,4 @@ MonoBehaviour:
abilityRadius: 4 abilityRadius: 4
abilityDuration: 3 abilityDuration: 3
abilityMagnitude: 0.3 abilityMagnitude: 0.3
slowZonePrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} abilityPrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3}

@ -69,3 +69,8 @@ MonoBehaviour:
SourcePrefabToOverride: {fileID: 0} SourcePrefabToOverride: {fileID: 0}
SourceHashToOverride: 0 SourceHashToOverride: 0
OverridingTargetPrefab: {fileID: 0} OverridingTargetPrefab: {fileID: 0}
- Override: 0
Prefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3}
SourcePrefabToOverride: {fileID: 0}
SourceHashToOverride: 0
OverridingTargetPrefab: {fileID: 0}

@ -118,5 +118,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 16c014692584a9a438b902616cc2dd35, type: 3} m_Script: {fileID: 11500000, guid: 16c014692584a9a438b902616cc2dd35, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
zoneRadius: 5 Ability: {fileID: 11400000, guid: e4794e2f71f66a74486c797344695ce7, type: 2}
slowDuration: 3

@ -10,13 +10,19 @@ public abstract class Ability : ScriptableObject
public float abilityRadius; public float abilityRadius;
public float abilityDuration; public float abilityDuration;
public float abilityMagnitude; public float abilityMagnitude;
[SerializeField]
private GameObject abilityPrefab; // Prefab associated with this ability
public GameObject GetPrefab()
{
return abilityPrefab;
}
public void ActivateAbility(GameObject owner) public void ActivateAbility(GameObject owner)
{ {
Activate(owner); Activate(owner);
} }
protected abstract void Activate(GameObject owner); // Logic for the specific ability protected abstract void Activate(GameObject owner); // Logic for the specific ability
}
public abstract void OnDrawAbilityGizmos(Vector3 pos);
}

@ -1,8 +1,9 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine; using UnityEngine;
public class AbilitySystem : MonoBehaviour public class AbilitySystem : NetworkBehaviour
{ {
[Header("Assigned Abilities")] [Header("Assigned Abilities")]
public List<Ability> abilities = new List<Ability>(); public List<Ability> abilities = new List<Ability>();
@ -12,6 +13,7 @@ public class AbilitySystem : MonoBehaviour
private HashSet<Ability> abilitiesOnCooldown = new HashSet<Ability>(); private HashSet<Ability> abilitiesOnCooldown = new HashSet<Ability>();
[SerializeField] private GameObject currentAbilityIndicator; [SerializeField] private GameObject currentAbilityIndicator;
void Update() void Update()
{ {
if (isAbilityActive) if (isAbilityActive)
@ -52,7 +54,7 @@ public class AbilitySystem : MonoBehaviour
{ {
return isAbilityActive; // Returns true if an ability mode is currently active return isAbilityActive; // Returns true if an ability mode is currently active
} }
private void ToggleAbilityMode(Ability ability) private void ToggleAbilityMode(Ability ability)
{ {
if (isAbilityActive && activeAbility == ability) if (isAbilityActive && activeAbility == ability)
@ -85,7 +87,8 @@ public class AbilitySystem : MonoBehaviour
{ {
if (activeAbility != null) if (activeAbility != null)
{ {
activeAbility.ActivateAbility(gameObject); var spawnPosition = currentAbilityIndicator.transform.position;
SpawnAbilityServerRpc(activeAbility.abilityKey, spawnPosition);
StartCoroutine(StartCooldown(activeAbility)); StartCoroutine(StartCooldown(activeAbility));
DeactivateAbilityMode(); DeactivateAbilityMode();
} }
@ -105,7 +108,40 @@ public class AbilitySystem : MonoBehaviour
Debug.Log($"{ability.abilityName} is off cooldown."); Debug.Log($"{ability.abilityName} is off cooldown.");
} }
/// <summary>
/// Spawns the requested ability on the server.
/// </summary>
[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<NetworkObject>();
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() private void UpdateIndicatorPosition()
{ {
// Raycast to get the position of the cursor on the ground // 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)) if (Physics.Raycast(ray, out RaycastHit hit))
{ {
currentAbilityIndicator.transform.position = hit.point; 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);
// }
// }
// }
} }

@ -1,43 +1,11 @@
using UnityEngine; using UnityEngine;
using Unity.Netcode;
[CreateAssetMenu(menuName = "Abilities/Slow Zone Ability")] [CreateAssetMenu(menuName = "Abilities/Slow Zone Ability")]
public class SlowZoneAbility : Ability public class SlowZoneAbility : Ability
{ {
[Header("Slow Zone Settings")]
public GameObject slowZonePrefab;
protected override void Activate(GameObject owner) protected override void Activate(GameObject owner)
{ {
Camera mainCamera = Camera.main; // No direct logic here, spawning is handled in AbilitySystem
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); Debug.Log($"SlowZoneAbility activated by {owner.name}.");
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<NetworkObject>().Spawn();
slowZone.GetComponent<SlowZonePrefab>().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);
} }
} }

@ -24,7 +24,7 @@ public class SlowZonePrefab : NetworkBehaviour
yield return new WaitForSeconds(Ability.abilityDuration); yield return new WaitForSeconds(Ability.abilityDuration);
// Destroy the zone after applying the slow effect // Destroy the zone after applying the slow effect
Destroy(gameObject); DespawnZone();
} }
private void ApplySlowToPlayers() private void ApplySlowToPlayers()
@ -60,6 +60,23 @@ public class SlowZonePrefab : NetworkBehaviour
Destroy(gameObject); Destroy(gameObject);
} }
private void DespawnZone()
{
if (IsServer)
{
var networkObject = GetComponent<NetworkObject>();
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 // Debug visualization for the slow zone in the editor
private void OnDrawGizmos() private void OnDrawGizmos()
{ {

Loading…
Cancel
Save