You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/SlowZonePrefab.cs

87 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
public class SlowZonePrefab : NetworkBehaviour
{
[Header("Slow Zone Settings")]
public Ability Ability;
private void Start()
{
// Start the process to slow down players when the zone is spawned
StartCoroutine(ApplySlowEffect());
}
private IEnumerator ApplySlowEffect()
{
// Run the logic once immediately
ApplySlowToPlayers();
// Optionally, repeat the check while the zone exists
yield return new WaitForSeconds(Ability.abilityDuration);
// Destroy the zone after applying the slow effect
DespawnZone();
}
private void ApplySlowToPlayers()
{
// Find all colliders in the zone
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Ability.abilityRadius);
foreach (var collider in hitColliders)
{
if (collider.TryGetComponent<ServerCharacter>(out var 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!");
}
}
}
}
private IEnumerator SlowPlayer(ServerCharacter player)
{
// Halve the player's movement speed
player.Movement.SetSpeedModifier(Ability.abilityMagnitude);
// Wait for the slow duration
yield return new WaitForSeconds(Ability.abilityDuration);
// Restore the original speed
1 month ago
player.Movement.ResetSpeedModifier();
Debug.Log($"{player.name}'s speed is restored.");
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()
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, Ability.abilityRadius);
}
}