Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Daniel Chicchon |
Hi everyone, first time poster.
For my first project, I am building a lightbike game and am having trouble with how to set a direction for the player with only two keys.
Here is my code so far:
var screen_size var score : int = 0
var speed : int = 300
var velocity : Vector2 = Vector2()
onready var sprite : Sprite = get_node("Sprite")
onready var camera : Camera = get_node("Camera2D")
func _ready():
screen_size = get_viewport_rect().size
pass
func _physics_process(delta):
if Input.is_action_just_pressed("move_left"):
velocity = Vector2()
velocity.x = -speed
elif Input.is_action_just_pressed("move_right"):
velocity = Vector2()
velocity.x = speed
move_and_collide(velocity * delta)
Here I only want to use the left and right keys to move the player left or right relative to their current vector. Haven’t had much luck looking in the Vector2 docs so far so I figured I would ask the question here instead.