The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello,

In a 2d top down space game, I'm attempting to make a space ship move using input vectors (using heartbeast's ARPG tutorial). The ship rotates using the look_at function and faces the mouse cursor. How would I go about making the input vectors relative to the ship's rotation?

var input_vector = Vector2.ZERO 
input_vector.x = Input.get_action_strength("Right") - Input.get_action_strength("Left")
input_vector.y = Input.get_action_strength("Backward") - Input.get_action_strength("Forward")
input_vector = input_vector.normalized()

if input_vector!= Vector2.ZERO: 
    velocity += input_vector * acceleration * delta
    velocity = velocity.clamped(max_move_speed * delta)

else: 
    velocity = velocity.move_toward(Vector2.ZERO, friction * delta)

move_and_slide(velocity * delta)
Godot version 3.5.1
in Engine by (12 points)

1 Answer

0 votes
velocity += transform.basis.x * your_movement_calculation

(or change to suit whatever is considered forward; it could be basis.y in your game)

You don't need delta in move_and_slide, see here: https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html#move-and-slide

by (1,406 points)

Thanks for the response! I was able to use transform.basis_xform by doing the following:

input_vector = transform.basis_xform(input_vector).rotated($Sprite.rotation)

(had to use my sprite's rotation since it is rotated 90 degrees so that using look_at to my global mouse position works correctly)

But this seemed to do the trick! Thanks for the catch on delta in move_and_slide, that's very helpful.

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.