Refactor snapping logic in PlayerController to improve velocity correction and responsiveness

This commit is contained in:
2025-12-13 16:00:20 +01:00
parent 3a56ec7526
commit 4ae41b3ce9

View File

@@ -223,18 +223,20 @@ namespace Infrastructure.Unity
private void ApplySnapping(Vector3 axis) private void ApplySnapping(Vector3 axis)
{ {
if (!_isGrounded) return; if (!_isGrounded) return;
var targetPos = Vector3.Dot(_currentTileCentroid, axis); var targetPos = Vector3.Dot(_currentTileCentroid, axis);
var currentPos = Vector3.Dot(transform.position, 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 currentVelOnAxis = Vector3.Dot(rb.linearVelocity, axis);
var targetVel = diff * snapForce; var desiredVel = error * snapForce;
var correction = axis * (targetVel - currentVelOnAxis); var maxCatchupSpeed = Mathf.Abs(error) / Time.fixedDeltaTime;
desiredVel = Mathf.Clamp(desiredVel, -maxCatchupSpeed, maxCatchupSpeed);
var correction = axis * (desiredVel - currentVelOnAxis);
rb.AddForce(correction, ForceMode.VelocityChange); rb.AddForce(correction, ForceMode.VelocityChange);
} }