-
-
Save bitmammoth/b262ceb68b80b51f7624b64ee253deaf to your computer and use it in GitHub Desktop.
[scaffold] Area2D - Player movement
This file contains 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 Area2D | |
export var speed = 400 | |
var screensize | |
var velocity = Vector2() | |
func _ready(): | |
screensize = get_viewport_rect().size | |
func _process(delta): | |
var input_dir = Vector2() | |
if Input.is_key_pressed(KEY_UP): | |
input_dir.y -= 1.0 | |
if Input.is_key_pressed(KEY_DOWN): | |
input_dir.y += 1.0 | |
if Input.is_key_pressed(KEY_LEFT): | |
input_dir.x -= 1.0 | |
if Input.is_key_pressed(KEY_RIGHT): | |
input_dir.x += 1.0 | |
# Move our player to input direction | |
position += (speed * delta) * input_dir | |
# Check if we moved past the screen, if so just move us back | |
if position.x < 0.0: | |
position.x = 0.0 | |
elif position.x > screensize.x: | |
position.x = screensize.x | |
if position.y < 0.0: | |
position.y = 0.0 | |
elif position.y > screensize.y: | |
position.y = screensize.y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment