How can i get and transform the mouse angle to a int number?

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

I want to make a top down shooter with 360° aim animation like Metroid, but, how can transform the mouse angle position into 8 numbers? Like I want my character to be able to aim in 8 directions, in each direction he will change the sprite of aiming, like metroid, and I want a deadzone of 45° degress like, if he is aiming between 0 and 45 degrees, he will stay with the same sprite and keep rotating until he changes to the next zone. One more question, how can I stop the rotation to 0? When I try, It keeps rotating, I ended up making 2 sprites, one that always follows and is only visible when aiming and the sprite on top that is visible when stopped, but it’s too bad like that.

You can look at this: 8-Directional Movement/Animation :: Godot 3 Recipes

Enfyna | 2023-06-20 21:52

:bust_in_silhouette: Reply From: abdelhakim

this is an example for 8 directions you can clean the code to be small
transform the mouse angle position into 8 numbers
godot 3.5.2 stable

    extends Node2D
    func _ready():
    func _process(delta):
    	var hero=get_node("Sprite") # sprite node
    	var mouse_pos=get_global_mouse_position()
    	var sprite_pos=get_node("Sprite").global_position
    	var radian_angle_to_mouse=get_node("Sprite").global_position.angle_to_point(get_global_mouse_position())
    	var degree_angle_to_mouse=rad2deg(radian_angle_to_mouse)
    	if(degree_angle_to_mouse>-22.5 and degree_angle_to_mouse<22.5):
    	if(degree_angle_to_mouse>22.5 and degree_angle_to_mouse<67.5):
    	if(degree_angle_to_mouse>67.5 and degree_angle_to_mouse<112.5):
    	if(degree_angle_to_mouse>112.5 and degree_angle_to_mouse<157.5):
    	if(degree_angle_to_mouse>157.5 and degree_angle_to_mouse<180 or degree_angle_to_mouse>-157.5 and degree_angle_to_mouse<-180):
    	if(degree_angle_to_mouse>-157.5 and degree_angle_to_mouse<-112.5):
    	if(degree_angle_to_mouse>-112.5 and degree_angle_to_mouse<-67.5):
    	if(degree_angle_to_mouse>-67.5 and degree_angle_to_mouse<-22.5):
hero.rotation_degrees = stepify(degree_angle_to_mouse,45)

if I am not mistaken this line can replace all your if statements.

Enfyna | 2023-06-20 21:37