Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | CatRass |
For my game, I want the player to fly around, but for the controls I want up to move forward, down to move backwards, left to turn the player to the left, and right to turn the player to the right. I want the turning to just turn the controlled KinematicBody2D. I’ve gotten this code, but don’t know how to make it work the way I’d want it to.
extends KinematicBody2D
export (int) var speed = 200
export (float) var rotation_speed = 1.5
var velocity = Vector2()
var rotation_dir = 0
func get_input():
rotation_dir = 0
velocity = Vector2()
if Input.is_action_pressed('right'):
rotation_dir -= 1
if Input.is_action_pressed('left'):
rotation_dir += 1
if Input.is_action_pressed('down'):
velocity = Vector2(-speed, 0).rotated(rotation)
if Input.is_action_pressed('up'):
velocity = Vector2(speed, 0).rotated(rotation)
func _physics_process(delta):
get_input()
rotation += rotation_dir * rotation_speed * delta
velocity = move_and_slide(velocity)