Trying to make player movement relative to player rotation, but can't find node position correctly

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

Hi y’all, I’m currently trying to make a jetpack for a 2D game that allows you to fly left right up down and rotate left and right as well. I’d like the inputs for left right up and down to be relative to this rotation, so after reading the docs on vector math I came up with the following code: (floor_checker is an area2d node I’m using to check whether the player is standing on a surface and is therefore located a bit below the player, making it useful for determining the rotation)

if Input.is_action_pressed("jetpack down"):
     velocity = (floor_checker.position - self.position).normalized() * jetpack_scalar

the above example however just caused the player to move towards the (0, 0) point. So I assumed that was because it was referencing the player scene, where the player is at (0, 0) and the floorchecker is barely any farther. So I decided I would get the nodes from the perspective of the game’s root:

onready var self_center = get_tree().get_root().get_node("Root/Game/Player")
onready var floor_checker = get_tree().get_root().get_node("Root/Game/Player/FloorChecker/CollisionShape2D")

and then use those to get the actual positions I need:

if Input.is_action_pressed("jetpack_down"):
     velocity = (floor_checker.position - self_center.position).normalized() * jetpack_scalar

which unfortunately produced the same result. I’m not sure now how I’m supposed to measure the position and/or rotation of the player character. How do I best go about this?

:bust_in_silhouette: Reply From: Gluon

There is a global position function so I would suggest using that e.g.

velocity = (floor_checker.global_position - self_center.global_position).normalized() * jetpack_scala

if you make those changes throughout this code it should fix it I think.