Rotation-Based Movement?

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

Hi! I’m making a game (of course), and I have a question. Essentially, the player has a “fist” that rotates towards the mouse (i have no problem with this part, it’s the next part I need help with), and when the player clicks, I want the player to move towards the direction that the “fist” was facing (not necessarily towards the position where the mouse was when it was clicked - if the user clicks nearby the player, I want the player to move in the direction of the mouse and keep going even after it has passed where the mouse was when clicked).

:bust_in_silhouette: Reply From: Skipperro

I think you could achieve this by using xform function and passing it a vector like Vector3(0,0,-1) (assuming you are in 3D space, Vector2 if you are in 2D).

The idea here is, that xformwill get the movement relative to the node, where -z axis is forward, and then return coordinates in global space, which you can use for movement, for example with move_and_slide() or setting linear_velocity of a rigid body.

I use this technique to fire a bolt from a crossbow, held by the character and the bolt should be fired in the current direction the crossbow is pointing, not towards mouse or player.