Making a KinematicBody2D rotate on the spot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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):
	rotation += rotation_dir * rotation_speed * delta
	velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: Becbunzen

Check this tutorial, I think it covers what you want to do:

:bust_in_silhouette: Reply From: deaton64


I’ve copied your code and other than having left and right the wrong way round, it works for me. Right should be +1 and left -1.

What isn’t it doing for you?