Register range of angles of virtual joystick as single direction for player movement

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

For a mobile platformer project, I’ve been attempting to create a virtual joystick that registers multiple angles or degrees as either left or right movement only. I’ve been trying to use a range of degrees of the virtual joystick that are then sent to a kinematicbody2d, but haven’t had any success so far. To illustrate further, if the joystick circle is comprised of 360 degrees with the right-most side being degree 0, traveling counter-clockwise around the circle, I’d like degrees 135 - 225 to register as LEFT and degrees 315 - 45 to register as RIGHT. I hope this makes sense. If anyone could point me in the right-most direction, it’d be truly valuable to me.

:bust_in_silhouette: Reply From: Lopy

func direction(degree):
. if degree >= 135 and degree <= 255:
. . return Vector2.LEFT
. if degree >= 315 or degree <= 45:
. . return Vector2.RIGHT
. return Vector2.ZERO

Thank you much. This seems very intuitive.

DamianTWinter | 2020-12-30 01:47