Skip to content

Instantly share code, notes, and snippets.

@27thLiz
Created January 29, 2016 19:31
Show Gist options
  • Save 27thLiz/4e782bdae732e888916c to your computer and use it in GitHub Desktop.
Save 27thLiz/4e782bdae732e888916c to your computer and use it in GitHub Desktop.
extends Sprite
var grid_size = 32
var speed = 64 # speed in pixels per second
var direction = Vector2(1, 0) # move in positive x direction, adapt this in your code for other directions
var is_moving = false
var moved_dist # keep track of how much we already moved
func _fixed_process(delta):
if Input.is_action_pressed("myInputAction") and !is_moving:
#set direction here
is_moving = true
moved_dist = 0.0
if is_moving:
if (moved_dist < grid_size):
#still have space, move on
var offset = direction * speed * delta
moved_dist += offset.length()
set_pos(get_pos() + offset)
else:
#time to stop moving
is_moving = false
func _ready():
set_fixed_process(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment