Find a point from sprite using distance and direction.

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

Hey! I’m building a Twin Stick shooter prototype for fun and learning.

Analog stick based movement with the left stick on my controller is working fine.

I’ve also got a little arrow sprite that is visible when the right stick is pressed, to show which direction you’re aiming. I’ve got this rotating in the direction the right stick is pointing just fine too.

The code for this looks like this:

if is_aiming(): #True if right stick is pressed in any direction.
		arrow.visible = true
		arrow.rotation = aim_direction.angle() #aim_direction is a normalized Vector2
		arrow.visible = false

Now what I want to do next is get the arrow to offset itself from the player sprite by a standard distance in that same aim_direction. But I can’t figure out how to do that.

Say I want the arrow sprite to display 16 pixels away from the player sprite in whatever direction the right stick is being held. How do I go about doing this? Can anyone point me in the right direction?

:bust_in_silhouette: Reply From: Bimbus

What I do is add the “arrow” node as a child to a “Direction” node and rotate the Direction node.

so set up like this:


Then offset Arrow to say 16 pixels to the right
when you want to rotate, rotate the Direction node. it will rotate but keep the offset

then you just have to remember that “right” is the default direction, so rotation = 0. of course you could set the offset to whatever makes more sense for your game, maybe “up” is the more natural default direction

:bust_in_silhouette: Reply From: p7f

If the arrow is placed as child of the character, you could try something like

arrow.position = 16*aim_direction

Assuming aim_direction is normalized.

I’m assuming the arrow is child of the character, and its pivot is on the starting point of the arrow.

This works exactly like I wanted it to, thank you.

cleeverz | 2020-08-25 17:01

Glad to help!

p7f | 2020-08-25 17:02