| 1 |
# meta-description: Classic movement for gravity games (platformer, ...) |
| 2 |
|
| 3 |
extends _BASE_ |
| 4 |
|
| 5 |
|
| 6 |
const SPEED = 300.0 |
| 7 |
const JUMP_VELOCITY = -400.0 |
| 8 |
|
| 9 |
|
| 10 |
func _physics_process(delta: float) -> void: |
| 11 |
# Add the gravity. |
| 12 |
if not is_on_floor(): |
| 13 |
velocity += get_gravity() * delta |
| 14 |
|
| 15 |
# Handle jump. |
| 16 |
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): |
| 17 |
velocity.y = JUMP_VELOCITY |
| 18 |
|
| 19 |
# Get the input direction and handle the movement/deceleration. |
| 20 |
# As good practice, you should replace UI actions with custom gameplay actions. |
| 21 |
var direction := Input.get_axis("ui_left", "ui_right") |
| 22 |
if direction: |
| 23 |
velocity.x = direction * SPEED |
| 24 |
else: |
| 25 |
velocity.x = move_toward(velocity.x, 0, SPEED) |
| 26 |
|
| 27 |
move_and_slide() |