Add hold-to-sprint with dedicated speed multiplier

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 4.8 committed 2026-06-22 21:46:57 +02:00
1 parent 70e5323125
commit 9e19146e35
1 file changed
+8 -1
+8 -1
View File
@@ -32,6 +32,9 @@ public class PlayerCharacterController : MonoBehaviour
[Tooltip("Horizontal movement speed (units/second).")]
public float moveSpeed = 4f;
[Tooltip("Speed multiplier applied to ground movement while the sprint key (Left Shift) is held.")]
public float sprintSpeedMultiplier = 1.8f;
[Tooltip("Interpret WASD relative to the camera (off = world X/Z axes).")]
public bool cameraRelative = true;
@@ -165,6 +168,7 @@ public class PlayerCharacterController : MonoBehaviour
{
float h = 0f, v = 0f;
bool jumpPressed = false;
bool sprinting = false;
// Skip input when control is disabled (e.g. paint mode); gravity and
// grounding below still run so the player stays planted.
@@ -179,11 +183,13 @@ public class PlayerCharacterController : MonoBehaviour
if (keyboard.sKey.isPressed) v -= 1f;
if (keyboard.wKey.isPressed) v += 1f;
jumpPressed = keyboard.spaceKey.wasPressedThisFrame;
sprinting = keyboard.leftShiftKey.isPressed;
}
#else
h = Input.GetAxisRaw("Horizontal");
v = Input.GetAxisRaw("Vertical");
jumpPressed = Input.GetButtonDown("Jump");
sprinting = Input.GetKey(KeyCode.LeftShift);
#endif
}
@@ -265,7 +271,8 @@ public class PlayerCharacterController : MonoBehaviour
_slideSpeed = 0f;
}
Vector3 velocity = moveDir * moveSpeed + slideVelocity + Vector3.up * _verticalVelocity;
float currentSpeed = sprinting ? moveSpeed * sprintSpeedMultiplier : moveSpeed;
Vector3 velocity = moveDir * currentSpeed + slideVelocity + Vector3.up * _verticalVelocity;
// Move fires OnControllerColliderHit, which refills the ground normal;
// fall back to "up" if nothing was hit.