Godot 2d Platformer Move and Jump

Mar 16, 2021
extends KinematicBody2Dconst GRAVITY = 600const WALK_SPEED = 200const JUMP_FORCE = 500var velocity = Vector2()var screen_sizefunc _ready():    screen_size = get_viewport_rect().size    func _physics_process(delta):    velocity.y += delta * GRAVITY        if Input.is_action_pressed("ui_left"):        velocity.x = -WALK_SPEED    elif Input.is_action_pressed("ui_right"):        velocity.x = WALK_SPEED    else:        # velocity.x = 0        # smoothen the stop        velocity.x = lerp(velocity.x, 0, 0.1)        if Input.is_action_pressed("ui_up") and is_on_floor():        velocity.y = -JUMP_FORCE         velocity = move_and_slide(velocity, Vector2.UP)        # prevent player going out of screen    position.x = clamp(position.x, 0, screen_size.x)    position.y = clamp(position.y, 0, screen_size.y)

null

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.