• 2D
  • How could I change animations based on where the mouse is on the screen?

I am making a game where the player spins around on the screen to face the mouse, except instead of actually turning the sprite, there are a lot of different animations that look like the player is spinning, so I was wondering how to play a different animation based on the mouse position. I am brand spankin' new to Godot, loving it so far, there's just some stuff I don't know how to do, like this.

This discussion was caught in the moderation queue since you have not confirmed your account yet.

Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.

(Note: You do not need to repost, the discussion has been moved out and is visible now)

If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

There's an example of the spin. Each direction has a different animation, those aren't shown in the gif

9 days later

In this case I wouldnt use animations but change the sprites texture manually based on the mouse position percentage ie

pseudo code:

func handle_mouse(event):
	if event is InputEventMouseMotion:
		mouse_position = event.position
		screen_width = get_viewport.size.x
		screen_height = get_viewport.size.y
		x_percent = mouse_position.x/screen_width
		y_percent = mouse_position.y/screen_width
		change_sprite(x_percent,y_percent)
func change_sprite(x_percent,y_percent):
	#left side
	if x_percent < 0.5: #you can change this to a range 
		#based on how many left side sprites you have use the
		# lower 50% of x_percent and the y_percent value to determine what sprite to change to
	#right side
	elif x_percent > 0.5: #you can change this to a range 
		#based on how many right side sprites you have use the
		# upper 50% of x_percent and the y_percent value to determine what sprite to change to
	else: #x_percent is equal to 50%  or within range
		if y_percent > 0.5:
			#assign up sprite or vise versa
		else:
			#assign down sprite  or vise versa

theres probably a much better way to do this but its the most straight forward way I can think of