refactor (slowndow): create a function in movement script to apply the slowdown 'keep the logic'

This commit is contained in:
Pierre Ryssen
2026-05-01 21:53:19 +02:00
parent f120b487ec
commit 94c28dca2f
2 changed files with 15 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ public class SlowdownArea : MonoBehaviour
public float slowSpeed; public float slowSpeed;
public float timer; public float timer;
private float baseSpeed;
private float baseJump; private float baseJump;
private bool isInZone = false; private bool isInZone = false;
private PlayerMovement player; private PlayerMovement player;
@@ -19,7 +19,6 @@ public class SlowdownArea : MonoBehaviour
if (area.CompareTag("Player")){ if (area.CompareTag("Player")){
player = area.GetComponent<PlayerMovement>(); player = area.GetComponent<PlayerMovement>();
jump = area.GetComponent<PlayerJump>(); jump = area.GetComponent<PlayerJump>();
baseSpeed = player.WalkSpeed;
baseJump = 5.0f; baseJump = 5.0f;
jump.JumpForce = 1.5f; jump.JumpForce = 1.5f;
isInZone = true; isInZone = true;
@@ -32,7 +31,7 @@ public class SlowdownArea : MonoBehaviour
{ {
isInZone = false; isInZone = false;
timer = 0f; timer = 0f;
player.WalkSpeed = baseSpeed; player.ApplySlow(1f);
jump.JumpForce = baseJump; jump.JumpForce = baseJump;
} }
} }
@@ -46,6 +45,6 @@ public class SlowdownArea : MonoBehaviour
timer += Time.deltaTime; timer += Time.deltaTime;
float t = Mathf.Clamp(timer * slowSpeed, 0f, 1f); float t = Mathf.Clamp(timer * slowSpeed, 0f, 1f);
float multiplicator = Mathf.Lerp(1f, maxSlow, t); float multiplicator = Mathf.Lerp(1f, maxSlow, t);
player.WalkSpeed = baseSpeed * multiplicator; player.ApplySlow(multiplicator);
} }
} }

View File

@@ -14,12 +14,17 @@ public class PlayerMovement : MonoBehaviour
private Vector3 moveDirection; private Vector3 moveDirection;
// used for root enemies
private float baseWalkSpeed;
private float slowMultiplier = 1f;
private void Awake() private void Awake()
{ {
m_rigidbody = GetComponent<Rigidbody>(); m_rigidbody = GetComponent<Rigidbody>();
input = GetComponent<PlayerInputController>(); input = GetComponent<PlayerInputController>();
animator = GetComponent<Animator>(); animator = GetComponent<Animator>();
headController = GetComponent<PlayerHeadController>(); headController = GetComponent<PlayerHeadController>();
baseWalkSpeed = WalkSpeed;
if (m_rigidbody != null) if (m_rigidbody != null)
{ {
@@ -27,6 +32,11 @@ public class PlayerMovement : MonoBehaviour
} }
} }
public void ApplySlow(float multiplier)
{
slowMultiplier = multiplier;
}
private void FixedUpdate() private void FixedUpdate()
{ {
Vector2 m_moveAmt = input.MoveAmount; Vector2 m_moveAmt = input.MoveAmount;
@@ -48,7 +58,7 @@ public class PlayerMovement : MonoBehaviour
if (headController.isHoldingHead) if (headController.isHoldingHead)
{ {
m_rigidbody.MovePosition( m_rigidbody.MovePosition(
m_rigidbody.position + Time.deltaTime * WalkSpeed * moveDirection m_rigidbody.position + Time.deltaTime * baseWalkSpeed * slowMultiplier * moveDirection
); );
} }
else else
@@ -56,7 +66,7 @@ public class PlayerMovement : MonoBehaviour
if (moveDirection.magnitude >= 0.1f) if (moveDirection.magnitude >= 0.1f)
{ {
m_rigidbody.MovePosition( m_rigidbody.MovePosition(
m_rigidbody.position + Time.deltaTime * WalkSpeed * moveDirection m_rigidbody.position + Time.deltaTime * baseWalkSpeed * slowMultiplier * moveDirection
); );
Quaternion targetRotation = Quaternion.LookRotation(moveDirection); Quaternion targetRotation = Quaternion.LookRotation(moveDirection);