Skip to content

Instantly share code, notes, and snippets.

@scott-wilson
Created December 11, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scott-wilson/892d782c3b582503ca0b4a6eeaa642db to your computer and use it in GitHub Desktop.
Save scott-wilson/892d782c3b582503ca0b4a6eeaa642db to your computer and use it in GitHub Desktop.
Movement controller for a character
extends KinematicBody2D
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
var velocity = Vector2()
const WALK_SPEED = 200.0
func _ready():
# Called every time the node is added to the scene.
# Initialization here
set_fixed_process(true)
func _fixed_process(delta):
velocity.x = 0.0
velocity.y = 0.0
if (Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right")):
velocity.x = 0.0
elif (Input.is_action_pressed("ui_left")):
velocity.x = -1.0
elif (Input.is_action_pressed("ui_right")):
velocity.x = 1.0
if (Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down")):
velocity.y = 0.0
elif (Input.is_action_pressed("ui_up")):
velocity.y = -1.0
elif (Input.is_action_pressed("ui_down")):
velocity.y = 1.0
var motion = velocity.normalized() * delta * WALK_SPEED
move(motion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment