How to make input vector relative to character facing?

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

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)
:bust_in_silhouette: Reply From: spaceyjase
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: Using CharacterBody2D/3D — Godot Engine (stable) documentation in English

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.

Gilberticus | 2023-01-24 21:06