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):
    rotation += rotation_dir * rotation_speed * delta
    velocity = move_and_slide(velocity)
in Engine by (51 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

by (160 points)
0 votes


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?

by (2,047 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.