Skip to content

Instantly share code, notes, and snippets.

@lamarmarshall
Created November 13, 2024 01:56
Show Gist options
  • Select an option

  • Save lamarmarshall/96725a33d5b7eaf7da87dd177427419c to your computer and use it in GitHub Desktop.

Select an option

Save lamarmarshall/96725a33d5b7eaf7da87dd177427419c to your computer and use it in GitHub Desktop.
godot 4, top down movement
extends CharacterBody2D
var speed: int = 100
var looking_left: bool = true
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
player_movement()
player_animation()
func player_animation() -> void:
if velocity.x > 0 and looking_left:
$Sprite2D.flip_h = !$Sprite2D.flip_h
looking_left = !looking_left
elif velocity.x < 0 and !looking_left:
$Sprite2D.flip_h = !$Sprite2D.flip_h
looking_left = !looking_left
func player_movement() -> void:
var input = Input.get_vector("left", "right", "up", "down")
velocity = input.normalized() * speed
move_and_slide()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment