M1: smooth player movement — ramp velocity (accel/decel) not instant

Instant full-speed-on-press / dead-stop-on-release was the jerky/jarring feel
and made the anim pop Idle->Run. Now MoveTowards-ramps horizontal velocity
(accel 45, decel 60 m/s^2), so starts/stops ease and Idle->Walk->Run blends.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
megaproxy 2026-07-12 18:52:44 +01:00
parent d83452a315
commit 44048708b8

View file

@ -33,6 +33,12 @@ namespace Jankenbots.Prototype
[Tooltip("Run speed in m/s.")]
public float moveSpeed = 5f;
[Tooltip("How fast the avatar ramps UP to full speed (m/s^2). Lower = floatier start.")]
public float acceleration = 45f;
[Tooltip("How fast the avatar ramps DOWN to a stop (m/s^2). Higher = snappier stop.")]
public float deceleration = 60f;
[Tooltip("How fast the avatar turns to face its movement direction (deg/s).")]
public float turnSpeed = 720f;
@ -46,6 +52,7 @@ namespace Jankenbots.Prototype
CharacterController _cc;
Animator _anim;
float _vy; // vertical velocity (gravity)
Vector3 _horizVel; // smoothed horizontal velocity (ramped, not instant)
Vector3 _lastPos;
float _animSpeed;
static readonly int SpeedHash = Animator.StringToHash("Speed");
@ -98,16 +105,23 @@ namespace Jankenbots.Prototype
Vector3 input = new Vector3(h, 0f, v);
if (input.sqrMagnitude > 1f) input.Normalize();
Vector3 move = input * moveSpeed;
Vector3 desired = input * moveSpeed;
// Ramp velocity toward the target instead of snapping — this is what kills
// the "jerky/jarring" feel and lets the anim blend Idle→Walk→Run smoothly.
float rate = input.sqrMagnitude > 0.001f ? acceleration : deceleration;
_horizVel = Vector3.MoveTowards(_horizVel, desired, rate * Time.deltaTime);
if (_cc.isGrounded && _vy < 0f) _vy = -2f;
_vy += gravity * Time.deltaTime;
_cc.Move((move + new Vector3(0f, _vy, 0f)) * Time.deltaTime);
_cc.Move((_horizVel + new Vector3(0f, _vy, 0f)) * Time.deltaTime);
if (move.sqrMagnitude > 0.01f)
// Face the way we're actually moving (uses the smoothed velocity, so turns
// ease too rather than snapping on a key change).
if (_horizVel.sqrMagnitude > 0.05f)
{
Quaternion target = Quaternion.LookRotation(new Vector3(move.x, 0f, move.z));
Quaternion target = Quaternion.LookRotation(new Vector3(_horizVel.x, 0f, _horizVel.z));
transform.rotation = Quaternion.RotateTowards(transform.rotation, target, turnSpeed * Time.deltaTime);
}
}