feat (rootEnemies): create a slowdown area that slow the player
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user