Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- var input_direction = 0
- var direction = 1
- var speed = 0
- const MAX_SPEED = 800
- const ACCELERATION = 2000
- const DECELERATION = 8000
- var velocity = 0
- func _ready():
- set_process(true)
- set_process_input(true)
- func _input(event):
- var move_left = event.is_action_pressed("move_left")
- var move_right = event.is_action_pressed("move_right")
- var stop_moving = not (Input.is_action_pressed("move_right") or Input.is_action_pressed("move_left"))
- if move_left:
- input_direction = -1
- elif move_right:
- input_direction = 1
- elif stop_moving:
- input_direction = 0
- if move_left or move_right and input_direction:
- if input_direction == -1 * direction:
- speed /= 3
- direction = input_direction
- func _process(delta):
- if input_direction:
- speed += ACCELERATION * delta
- else:
- speed -= DECELERATION * delta
- speed = clamp(speed, 0, MAX_SPEED)
- velocity = speed * delta * direction
- move(Vector2(velocity, 0))
Add Comment
Please, Sign In to add comment