0 votes

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)
asked Jun 3 in Engine by CatRass (45 points)

2 Answers

0 votes

Check this tutorial, I think it covers what you want to do: https://electronstudio.github.io/godot_space/tutorial.html

answered Jun 3 by Becbunzen (156 points)
0 votes

Hi,

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?

answered Jun 3 by deaton64 (1,201 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.