Advertisement
otorp2

grid move kinematic

Jan 29th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. extends Sprite
  2.  
  3. var grid_size = 32
  4. var speed = 64 # speed in pixels per second
  5. var direction = Vector2(1, 0) # move in positive x direction, adapt this in your code for other directions
  6. var is_moving = false
  7. var moved_dist # keep track of how much we already moved
  8.  
  9. func _fixed_process(delta):
  10. if Input.is_action_pressed("myInputAction") and !is_moving:
  11. #set direction here
  12. is_moving = true
  13. moved_dist = 0.0
  14. if is_moving:
  15. if (moved_dist < grid_size):
  16. #still have space, move on
  17. var offset = direction * speed * delta
  18. moved_dist += offset.length()
  19. set_pos(get_pos() + offset)
  20. else:
  21. #time to stop moving
  22. is_moving = false
  23.  
  24. func _ready():
  25. set_fixed_process(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement