Made the ability area based

dev-hazimBeforeMerge
Hazim Bin Ijaz 4 weeks ago
parent 71bc52f824
commit 8d3809fc1d

@ -16,7 +16,7 @@ MonoBehaviour:
abilityKey: Slowdown abilityKey: Slowdown
keybind: 113 keybind: 113
cooldownTime: 3 cooldownTime: 3
abilityRadius: 4 abilityRadius: 3
abilityDuration: 3 abilityDuration: 3
abilityMagnitude: 0.3 abilityMagnitude: 0.3
abilityPrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3} abilityPrefab: {fileID: 5818330108676053786, guid: 4979352732bf84b44a9c789bef80b18a, type: 3}

@ -13,6 +13,7 @@ GameObject:
- component: {fileID: 825873091886110301} - component: {fileID: 825873091886110301}
- component: {fileID: 8294231852451494523} - component: {fileID: 8294231852451494523}
- component: {fileID: 7596904637393423818} - component: {fileID: 7596904637393423818}
- component: {fileID: 1396255635289861961}
m_Layer: 0 m_Layer: 0
m_Name: SlowDownZonePrefab m_Name: SlowDownZonePrefab
m_TagString: Untagged m_TagString: Untagged
@ -97,7 +98,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
GlobalObjectIdHash: 725408325 GlobalObjectIdHash: 1238939516
InScenePlacedSourceGlobalObjectIdHash: 0 InScenePlacedSourceGlobalObjectIdHash: 0
AlwaysReplicateAsRoot: 0 AlwaysReplicateAsRoot: 0
SynchronizeTransform: 1 SynchronizeTransform: 1
@ -119,3 +120,25 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
Ability: {fileID: 11400000, guid: e4794e2f71f66a74486c797344695ce7, type: 2} 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}

@ -3,61 +3,83 @@ using System.Collections.Generic;
using Unity.BossRoom.Gameplay.GameplayObjects.Character; using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode; using Unity.Netcode;
using UnityEngine; using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(SphereCollider))]
public class SlowZonePrefab : NetworkBehaviour public class SlowZonePrefab : NetworkBehaviour
{ {
[Header("Slow Zone Settings")] [Header("Slow Zone Settings")]
public Ability Ability; public Ability Ability;
public GameObject visualEffectPrefab; // Prefab for visualizing the area
private HashSet<ServerCharacter> affectedPlayers = new HashSet<ServerCharacter>();
private SphereCollider zoneCollider;
private GameObject visualEffectInstance;
private void Start() private void Start()
{ {
// Start the process to slow down players when the zone is spawned zoneCollider = GetComponent<SphereCollider>();
StartCoroutine(ApplySlowEffect());
}
private IEnumerator ApplySlowEffect() // Spawn the visual effect
if (visualEffectPrefab)
{ {
// Run the logic once immediately visualEffectInstance = Instantiate(visualEffectPrefab, transform.position, Quaternion.identity);
ApplySlowToPlayers(); 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);
// Destroy the zone after applying the slow effect // Automatically destroy after the ability duration
DespawnZone(); StartCoroutine(DelayedDestroy());
} }
private void ApplySlowToPlayers() private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent<ServerCharacter>(out var player))
{ {
// Find all colliders in the zone if (!player.IsCrow && affectedPlayers.Add(player)) // Only affect non-crow players
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Ability.abilityRadius);
foreach (var collider in hitColliders)
{ {
if (collider.TryGetComponent<ServerCharacter>(out var player)) player.Movement.SetSpeedModifier(Ability.abilityMagnitude);
Debug.Log($"{player.name} entered the slow zone. Speed reduced.");
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.TryGetComponent<ServerCharacter>(out var player))
{ {
// Check if the player is NOT the crow if (affectedPlayers.Remove(player))
if (!player.IsCrow)
{ {
// Apply slow effect player.Movement.ResetSpeedModifier();
StartCoroutine(SlowPlayer(player)); Debug.Log($"{player.name} exited the slow zone. Speed restored.");
Debug.Log($"{player.name} is slowed down!");
} }
} }
} }
private void OnDestroy()
{
// 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.");
} }
private IEnumerator SlowPlayer(ServerCharacter player) affectedPlayers.Clear();
// Destroy the visual effect
if (visualEffectInstance)
{ {
// Halve the player's movement speed Destroy(visualEffectInstance);
player.Movement.SetSpeedModifier(Ability.abilityMagnitude); }
}
// Wait for the slow duration private IEnumerator DelayedDestroy()
{
yield return new WaitForSeconds(Ability.abilityDuration); yield return new WaitForSeconds(Ability.abilityDuration);
// Restore the original speed if (IsServer)
player.Movement.ResetSpeedModifier(); {
Debug.Log($"{player.name}'s speed is restored."); DespawnZone();
Destroy(gameObject); }
} }
private void 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);
}
} }

Loading…
Cancel
Save