Created
November 13, 2024 01:56
-
-
Save lamarmarshall/96725a33d5b7eaf7da87dd177427419c to your computer and use it in GitHub Desktop.
godot 4, top down movement
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 | |
| 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