Making script favor the larger number (regardless of negative)

Godot Version

4.2

Question

Hi! I’m quite new to programming, and I need help on a certain piece of code.
I’m making a pixelart roguelike game, and I’m designing all direction sprites by hand, so it’s a different sprite if the player faces left, right, down or up, and I want to make the same thing for my attack animations.

I’m currently getting the Player’s mouse position using the var attack_position = (get_local_mouse_position()).normalized(), and I want to make it so that the attack animation plays if the mouse is pointing in a certain direction, but I’m not quite sure how to tell the code which direction the player is pointing at.

My current code is this:

	var attack_position = (get_local_mouse_position()).normalized()
	if attack_position.x > attack_position.y:
		if attack_position.x > 0:
			print("right")
		else:
			print("left")
	else:
		if attack_position.y > 0:
			print("down")
		else:
			print("up")

The issue is in the first if.
I’m not quite sure how to tell the code to get the bigger number, regardless if it’s in the negative, if that makes sense (if x == -10, and y == 5, I still want it to pick x over y). How could I do this? Or is there a better way to make this entirely? Thank you in advance for the help.

You can use abs() function to get absolute value of number.

1 Like

Just tried it, and abs() works perfectly
Thank you so much for the quick answer!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.