0 votes

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?

Godot version 3.3.3
in Engine by (16 points)
edited by

1 Answer

0 votes

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.

by (1,685 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 webmaster@godotengine.org with your username.