Fixed the ability not syncing issue

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

@ -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}

@ -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}

@ -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}

@ -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);
}

@ -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<Ability> abilities = new List<Ability>();
@ -12,6 +13,7 @@ public class AbilitySystem : MonoBehaviour
private HashSet<Ability> abilitiesOnCooldown = new HashSet<Ability>();
[SerializeField] private GameObject currentAbilityIndicator;
void Update()
{
if (isAbilityActive)
@ -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,6 +108,39 @@ public class AbilitySystem : MonoBehaviour
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()
{
@ -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);
// }
// }
// }
}

@ -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<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);
// No direct logic here, spawning is handled in AbilitySystem
Debug.Log($"SlowZoneAbility activated by {owner.name}.");
}
}

@ -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<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
private void OnDrawGizmos()
{

Loading…
Cancel
Save