Advertisement
CodeTortoise

Godot 2D Char Movement with Run

Nov 22nd, 2015
2,786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3.  
  4. export var speed = 200.0
  5. export var run_speed = 300.0
  6.  
  7. var rad_up = 0
  8. var rad_down = PI
  9. var rad_right = (PI*3)/2
  10. var rad_left = PI/2
  11.  
  12.  
  13.  
  14. func process_movement(delta):
  15.     var motion = Vector2(0,0)
  16.    
  17.     if Input.is_action_pressed("move_up"):
  18.         motion += Vector2(0, -1)
  19.         motion = motion.normalized() * speed * delta
  20.         set_rot(rad_up)
  21.        
  22.     if Input.is_action_pressed("move_down"):
  23.         motion += Vector2(0, 1)
  24.         motion = motion.normalized() * speed * delta
  25.         set_rot(rad_down)
  26.  
  27.     if Input.is_action_pressed("move_right"):
  28.         motion += Vector2(1, 0)
  29.         motion = motion.normalized() * speed * delta
  30.         set_rot(rad_right)
  31.  
  32.     if Input.is_action_pressed("move_left"):
  33.         motion += Vector2(-1, 0)
  34.         motion = motion.normalized() * speed * delta
  35.         set_rot(rad_left)
  36.        
  37.     if Input.is_action_pressed("run_up"):
  38.         motion += Vector2(0, -1)
  39.         motion = motion.normalized() * run_speed * delta
  40.         set_rot(rad_up)
  41.        
  42.     if Input.is_action_pressed("run_down"):
  43.         motion += Vector2(0, 1)
  44.         motion = motion.normalized() * run_speed * delta
  45.         set_rot(rad_down)
  46.  
  47.     if Input.is_action_pressed("run_right"):
  48.         motion += Vector2(1, 0)
  49.         motion = motion.normalized() * run_speed * delta
  50.         set_rot(rad_right)
  51.  
  52.     if Input.is_action_pressed("run_left"):
  53.         motion += Vector2(-1, 0)
  54.         motion = motion.normalized() * run_speed * delta
  55.         set_rot(rad_left)
  56.    
  57.    
  58.     move(motion)
  59.  
  60. func _ready():
  61.     set_fixed_process(true)
  62.    
  63. func _fixed_process(delta):
  64.     process_movement(delta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement