From 4ae41b3ce9711ec29c501486fb0197098abd92ef Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Sat, 13 Dec 2025 16:00:20 +0100 Subject: [PATCH] Refactor snapping logic in PlayerController to improve velocity correction and responsiveness --- .../Infrastructure/Unity/PlayerController.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Infrastructure/Unity/PlayerController.cs b/Assets/Scripts/Infrastructure/Unity/PlayerController.cs index 38a067c..0162b13 100644 --- a/Assets/Scripts/Infrastructure/Unity/PlayerController.cs +++ b/Assets/Scripts/Infrastructure/Unity/PlayerController.cs @@ -223,18 +223,20 @@ namespace Infrastructure.Unity private void ApplySnapping(Vector3 axis) { if (!_isGrounded) return; - + var targetPos = Vector3.Dot(_currentTileCentroid, axis); var currentPos = Vector3.Dot(transform.position, axis); + var error = targetPos - currentPos; + + if (Mathf.Abs(error) < 0.05f) return; - var diff = targetPos - currentPos; - - var currentVelOnAxis = Vector3.Dot(rb.linearVelocity, axis); - var targetVel = diff * snapForce; - - var correction = axis * (targetVel - currentVelOnAxis); - + var desiredVel = error * snapForce; + + var maxCatchupSpeed = Mathf.Abs(error) / Time.fixedDeltaTime; + desiredVel = Mathf.Clamp(desiredVel, -maxCatchupSpeed, maxCatchupSpeed); + + var correction = axis * (desiredVel - currentVelOnAxis); rb.AddForce(correction, ForceMode.VelocityChange); }