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

73 lines
2.2 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 float zoneRadius = 5f; // Radius of the slow zone
public float slowDuration = 3f; // Duration of the slow effect
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(slowDuration);
// Destroy the zone after applying the slow effect
Destroy(gameObject);
}
private void ApplySlowToPlayers()
{
// Find all colliders in the zone
Collider[] hitColliders = Physics.OverlapSphere(transform.position, zoneRadius);
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
1 month ago
player.Movement.SetSpeedModifier(0.2f);
// player.Movement.SetMovementSpeed(originalSpeed * slowMultiplier);
// Wait for the slow duration
yield return new WaitForSeconds(slowDuration);
// Restore the original speed
1 month ago
player.Movement.ResetSpeedModifier();
Debug.Log($"{player.name}'s speed is restored.");
Destroy(gameObject);
}
// Debug visualization for the slow zone in the editor
private void OnDrawGizmos()
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, zoneRadius);
}
}