How to do movement based on rotation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aryn

I’m creating a asteroids clone, so the ship changes the rotation based on key inputs:

if rotate_left:
	rotation_degrees -= SPEED * delta;
	
if rotate_right:
	rotation_degrees += SPEED * delta;

Now I need him to move forward based on the direction he’s pointing, but I have no idea how to do that! I’m using Godot 3.0.2

:bust_in_silhouette: Reply From: kidscancode

Use the rotation to point your velocity vector. For example, if you’re using KinematicBody2D:

velocity = Vector2(1, 0).rotated(rotation) * run_speed
velocity = move_and_slide(velocity)

Yeah, I’m using KinematicBody2D. I tried this code, but it isnt working properly, and I didn’t get it so I don’t know how its supposed to work

My full code:

extends KinematicBody2D

const SPEED = 200
var velocity = Vector2()

func _ready():
rotation = PI
rotation_degrees = 0

func _process(delta):

var rotate_left = Input.is_action_pressed("ui_left");
var rotate_right = Input.is_action_pressed("ui_right");
var move_foward = Input.is_action_pressed("ui_up");
var shooting = Input.is_action_pressed("ui_shot");

if rotate_left:
	rotation_degrees -= SPEED * delta;
	
if rotate_right:
	rotation_degrees += SPEED * delta;

if move_foward:
	velocity = Vector2(1, 0).rotated(rotation) * SPEED * delta

velocity = move_and_slide(velocity)

Edit: he’s moving to the left side of the kinematic node

Edit2: Fixed! Now I got it that the velocity vector should be the position to the point. (changed it to Vector2(0,-1). Thanks a lot :slight_smile:

Aryn | 2018-03-20 21:06

A few things:

  • Semicolons are not needed in GDScript (I notice some of your lines have them)

  • rotation and rotation_degrees are the same property, just using radians or degrees, so setting them both in _ready() is redundant. Your code is setting rotation to 180 degrees and then 0.

  • Movement of physics bodies should always be done in _physics_process(), not _process()

I’m not sure what “he’s moving to the left side of the kinematic node” means. Can you clarify?

kidscancode | 2018-03-20 21:58

Ooh I knew about the semicolon thing but it’s like automatic to me, I’ll try to remember it

I’m not sure what “he’s moving to the left side of the kinematic node” means. Can you clarify?

AFAIK since the velocity vector was Vector2(1, 0), he’s pointing to the positive X direction, so he was moving to the right direction instead upward

Sorry for my english, unfortunately it isn’t my main language

Aryn | 2018-03-20 22:23

Is it necessary to include:

var rotate_left = Input.is_action_pressed(“ui_left”);
var rotate_right = Input.is_action_pressed(“ui_right”);
var move_foward = Input.is_action_pressed(“ui_up”);
var shooting = Input.is_action_pressed(“ui_shot”);

within _process(delta)?

It seems like that’s a waste of memory reassigning those variables every tick.

Just curious, new to all of this

talamander | 2022-04-17 20:23

It seems like that’s a waste of memory reassigning those variables every tick.

Yes, it is necessary to check the inputs every frame because the
player can change them any/every frame.

Deozaan | 2022-11-30 06:10