feat (rootEnemies): create a slowdown area that slow the player

This commit is contained in:
Pierre Ryssen
2026-04-29 23:16:46 +02:00
parent 7a4f79ac6b
commit 7f2c58f864
3 changed files with 247 additions and 5 deletions

View File

@@ -2,15 +2,45 @@ using UnityEngine;
public class SlowdownArea : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
[Header("References")]
public float maxSlow;
public float slowSpeed;
public float timer;
private float baseSpeed;
private bool isInZone = false;
private PlayerMovement player;
private void OnTriggerEnter(Collider area)
{
if (area.CompareTag("Player")){
player = area.GetComponent<PlayerMovement>();
baseSpeed = player.WalkSpeed;
isInZone = true;
}
}
private void OnTriggerExit(Collider area)
{
if (area.CompareTag("Player"))
{
isInZone = false;
timer = 0f;
player.WalkSpeed = baseSpeed;
}
}
// Update is called once per frame
void Update()
{
if (!isInZone || player == null)
return;
timer += Time.deltaTime;
float t = Mathf.Clamp(timer * slowSpeed, 0f, 1f);
float multiplicator = Mathf.Lerp(1f, maxSlow, t);
player.WalkSpeed = baseSpeed * multiplicator;
}
}