This script provides smooth movement and jumping mechanics for a 2D Unity game, featuring:
- Variable jump height based on button hold duration.
- Coyote time for more forgiving jumps.
- Smooth acceleration and deceleration for natural movement.
- Automatic character flipping based on movement direction (optional).
- Attach
SmoothCharacterControl.csto your character GameObject. - Ensure it has a Rigidbody2D and Collider2D.
- Mark the ground objects with the
Floortag. - Configure input mappings in Unity's Input Manager.
- Uses horizontal damping for smooth acceleration/deceleration.
- Adjustable movement speed via
MovementSettings. - Character flips based on movement direction.
- Coyote Time: Allows jumps shortly after leaving the ground.
- Jump Buffering: Press jump slightly before landing to still trigger a jump.
- Variable Height: Releases early = shorter jump.
- Controlled Fall: Increased gravity after peak for a natural descent.
| Parameter | Description | Default |
|---|---|---|
horizontalDamping |
Smoothness of movement changes | 0.1f |
acceleration |
Character's max speed | 10f |
| Parameter | Description | Default |
|---|---|---|
CoyoteTime |
Extra jump window after leaving ground | 0.15f |
MinJumpMultiplier |
Jump reduction when releasing early | 0.5f |
SpeedToFallOnceReachedMaxHeight |
Gravity boost after peak jump | 2.5f |
- Movement: Reads horizontal input, applies SmoothDamp for fluid motion.
- Jumping: Handles coyote time, jump buffering, and variable height.
- Adjust
Jumpforceto tweak jump height. - Modify
MovementSettings.accelerationfor speed changes. - Tune
JumpSettingsto refine jump feel.
- Unity 2020+
Fork, modify, and submit a PR if you'd like to improve the script!
Developed by Gustavo Segato Gazzeta.