RingOfRaces/KinematicBody2D.gd

28 lines
811 B
GDScript3
Raw Normal View History

2020-08-29 14:36:49 +00:00
extends KinematicBody2D
const GRAVITY = 0.0
const WALK_SPEED = 200
var velocity = Vector2()
func _physics_process(delta):
velocity.y += delta * GRAVITY
if Input.is_action_pressed("move_left"):
2020-08-29 14:36:49 +00:00
velocity.x = -WALK_SPEED
elif Input.is_action_pressed("move_right"):
2020-08-29 14:36:49 +00:00
velocity.x = WALK_SPEED
elif Input.is_action_pressed("move_up"):
2020-08-29 14:36:49 +00:00
velocity.y = -WALK_SPEED
elif Input.is_action_pressed("move_down"):
2020-08-29 14:36:49 +00:00
velocity.y = WALK_SPEED
else:
velocity.x = 0
velocity.y = 0
2020-08-29 14:36:49 +00:00
# We don't need to multiply velocity by delta because "move_and_slide" already takes delta time into account.
# The second parameter of "move_and_slide" is the normal pointing up.
# In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
move_and_slide(velocity, Vector2(0, -1))