From 8d3809fc1dc56d4bb5cb5387102f24979f94dd47 Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Tue, 24 Dec 2024 17:31:44 +0500 Subject: [PATCH] Made the ability area based --- .../Action/Abilities/SlowZoneAbility.asset | 2 +- .../Abilities/SlowDownZonePrefab.prefab | 25 ++++- Assets/Scripts/Gameplay/SlowZonePrefab.cs | 93 +++++++++++-------- 3 files changed, 79 insertions(+), 41 deletions(-) diff --git a/Assets/GameData/Action/Abilities/SlowZoneAbility.asset b/Assets/GameData/Action/Abilities/SlowZoneAbility.asset index 07dda9e..60ce60d 100644 --- a/Assets/GameData/Action/Abilities/SlowZoneAbility.asset +++ b/Assets/GameData/Action/Abilities/SlowZoneAbility.asset @@ -16,7 +16,7 @@ MonoBehaviour: abilityKey: Slowdown keybind: 113 cooldownTime: 3 - abilityRadius: 4 + abilityRadius: 3 abilityDuration: 3 abilityMagnitude: 0.3 abilityPrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} diff --git a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab index 46a5fc0..4f79b82 100644 --- a/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab +++ b/Assets/Prefabs/Abilities/SlowDownZonePrefab.prefab @@ -13,6 +13,7 @@ GameObject: - component: {fileID: 825873091886110301} - component: {fileID: 8294231852451494523} - component: {fileID: 7596904637393423818} + - component: {fileID: 1396255635289861961} m_Layer: 0 m_Name: SlowDownZonePrefab m_TagString: Untagged @@ -97,7 +98,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Name: m_EditorClassIdentifier: - GlobalObjectIdHash: 725408325 + GlobalObjectIdHash: 1238939516 InScenePlacedSourceGlobalObjectIdHash: 0 AlwaysReplicateAsRoot: 0 SynchronizeTransform: 1 @@ -119,3 +120,25 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Ability: {fileID: 11400000, guid: e4794e2f71f66a74486c797344695ce7, type: 2} + visualEffectPrefab: {fileID: 3894374618083898175, guid: 63d404b949e644e4a92db7444ae88671, type: 3} +--- !u!135 &1396255635289861961 +SphereCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5818330108676053786} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 1 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Radius: 3 + m_Center: {x: 0, y: 2.46, z: 0} diff --git a/Assets/Scripts/Gameplay/SlowZonePrefab.cs b/Assets/Scripts/Gameplay/SlowZonePrefab.cs index 4f57f8b..e540fba 100644 --- a/Assets/Scripts/Gameplay/SlowZonePrefab.cs +++ b/Assets/Scripts/Gameplay/SlowZonePrefab.cs @@ -3,61 +3,83 @@ using System.Collections.Generic; using Unity.BossRoom.Gameplay.GameplayObjects.Character; using Unity.Netcode; using UnityEngine; -using UnityEngine.AI; +[RequireComponent(typeof(SphereCollider))] public class SlowZonePrefab : NetworkBehaviour { [Header("Slow Zone Settings")] public Ability Ability; + public GameObject visualEffectPrefab; // Prefab for visualizing the area + + private HashSet affectedPlayers = new HashSet(); + private SphereCollider zoneCollider; + private GameObject visualEffectInstance; + private void Start() { - // Start the process to slow down players when the zone is spawned - StartCoroutine(ApplySlowEffect()); - } + zoneCollider = GetComponent(); - private IEnumerator ApplySlowEffect() - { - // Run the logic once immediately - ApplySlowToPlayers(); + // Spawn the visual effect + if (visualEffectPrefab) + { + visualEffectInstance = Instantiate(visualEffectPrefab, transform.position, Quaternion.identity); + visualEffectInstance.transform.localScale = Vector3.one * Ability.abilityRadius * 2f; // Adjust scale to match the radius + } - // Optionally, repeat the check while the zone exists - yield return new WaitForSeconds(Ability.abilityDuration); + // Automatically destroy after the ability duration + StartCoroutine(DelayedDestroy()); + } - // Destroy the zone after applying the slow effect - DespawnZone(); + private void OnTriggerEnter(Collider other) + { + if (other.TryGetComponent(out var player)) + { + if (!player.IsCrow && affectedPlayers.Add(player)) // Only affect non-crow players + { + player.Movement.SetSpeedModifier(Ability.abilityMagnitude); + Debug.Log($"{player.name} entered the slow zone. Speed reduced."); + } + } } - private void ApplySlowToPlayers() + private void OnTriggerExit(Collider other) { - // Find all colliders in the zone - Collider[] hitColliders = Physics.OverlapSphere(transform.position, Ability.abilityRadius); - foreach (var collider in hitColliders) + if (other.TryGetComponent(out var player)) { - if (collider.TryGetComponent(out var player)) + if (affectedPlayers.Remove(player)) { - // Check if the player is NOT the crow - if (!player.IsCrow) - { - // Apply slow effect - StartCoroutine(SlowPlayer(player)); - Debug.Log($"{player.name} is slowed down!"); - } + player.Movement.ResetSpeedModifier(); + Debug.Log($"{player.name} exited the slow zone. Speed restored."); } } } - private IEnumerator SlowPlayer(ServerCharacter player) + private void OnDestroy() { - // Halve the player's movement speed - player.Movement.SetSpeedModifier(Ability.abilityMagnitude); + // Restore speed for all players when the zone is destroyed + foreach (var player in affectedPlayers) + { + player.Movement.ResetSpeedModifier(); + Debug.Log($"{player.name}'s speed restored due to slow zone destruction."); + } + + affectedPlayers.Clear(); + + // Destroy the visual effect + if (visualEffectInstance) + { + Destroy(visualEffectInstance); + } + } - // Wait for the slow duration + private IEnumerator DelayedDestroy() + { yield return new WaitForSeconds(Ability.abilityDuration); - // Restore the original speed - player.Movement.ResetSpeedModifier(); - Debug.Log($"{player.name}'s speed is restored."); - Destroy(gameObject); + if (IsServer) + { + DespawnZone(); + } } private void DespawnZone() @@ -76,11 +98,4 @@ public class SlowZonePrefab : NetworkBehaviour } } } - - // Debug visualization for the slow zone in the editor - private void OnDrawGizmos() - { - Gizmos.color = Color.cyan; - Gizmos.DrawWireSphere(transform.position, Ability.abilityRadius); - } }