Last active
March 5, 2025 20:00
-
-
Save lamarmarshall/4af8a51c143379dd8b26d80401b515b2 to your computer and use it in GitHub Desktop.
godot, characterbody2d, move, character controller, platformer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
@export var move_speed: float = 200 | |
@export var acceleration: float = 50 | |
@export var braking: float = 20 | |
@export var gravity: float = 500 | |
@export var jump_force: float = 250 | |
@onready var sprite: Sprite2D = $Sprite | |
@onready var anim: AnimationPlayer = $AnimationPlayer | |
var move_inputs: float | |
func _physics_process(delta: float) -> void: | |
if not is_on_floor(): | |
velocity.y += gravity * delta | |
move_inputs = Input.get_axis("move_left", "move_right") | |
#movement | |
if move_inputs != 0: | |
velocity.x = lerp(velocity.x, move_inputs * move_speed, acceleration * delta) | |
else: | |
velocity.x = lerp(velocity.x, 0.0, braking * delta) | |
#velocity.x = move_inputs * move_speed | |
#jumping | |
if Input.is_action_just_pressed("jump") and is_on_floor(): | |
velocity.y = -jump_force | |
move_and_slide() | |
func _process(delta: float) -> void: | |
if velocity.x != 0: | |
sprite.flip_h = velocity.x > 0 | |
_manage_animation() | |
func _manage_animation() -> void: | |
if not is_on_floor(): | |
anim.play("jump") | |
elif move_inputs != 0: | |
anim.play("move") | |
else: | |
anim.play("idle") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment