Refactor snapping logic in PlayerController to improve velocity correction and responsiveness
This commit is contained in:
@@ -226,15 +226,17 @@ namespace Infrastructure.Unity
|
|||||||
|
|
||||||
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;
|
||||||
|
|
||||||
var diff = targetPos - currentPos;
|
if (Mathf.Abs(error) < 0.05f) return;
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user