Advertisement
gdquest

Platform #1 - Player horizontal movement

Jan 18th, 2017
2,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # player.gd
  2. extends KinematicBody2D
  3.  
  4. var input_direction = 0
  5. var direction = 0
  6.  
  7. var speed = 0
  8. const MAX_SPEED = 600
  9. const ACCELERATION = 1000
  10. const DECELERATION = 2000
  11. # Velocity is the X component of our motion vector.
  12. var velocity = 0
  13.  
  14. func _ready():
  15.     set_process(true)
  16.  
  17. func _process(delta):
  18.     # INPUT
  19.     # If the player pressed a key on the last tick,
  20.     # We set the character's direction to the input
  21.     if input_direction:
  22.         direction = input_direction
  23.    
  24.     if Input.is_action_pressed("ui_left"):
  25.         input_direction = -1
  26.     elif Input.is_action_pressed("ui_right"):
  27.         input_direction = 1
  28.     else:
  29.         input_direction = 0
  30.    
  31.    
  32.     # MOVEMENT
  33.     # If the player changed direction since last frame,
  34.     # it means the character will turn around.
  35.     # In that case, we lower the character's speed
  36.     if input_direction == - direction:
  37.         speed /= 3
  38.  
  39.     if input_direction:
  40.         speed += ACCELERATION * delta
  41.     else:
  42.         speed -= DECELERATION * delta
  43.  
  44.     speed = clamp(speed, 0, MAX_SPEED)
  45.    
  46.     velocity = speed * delta * direction
  47.     move(Vector2(velocity, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement