How to detect mouse is going up or down?

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

So, I want to make the Player node play a specific animation from AnimationPlayer whenever the Mouse cursor is going up or going down (Like in Platypus game). and also the sprite of the node will be static once the cursor is neither going up or down.

I tried using the _input(event) function and I’m having a performance issue where the game lag out so bad while moving my cursor towards up and down, so is there an alternative way to do it?

also just in case, the Player position is following the cursor position.

:bust_in_silhouette: Reply From: Snudgley

The input function is your best bet for this perhaps you are running too much code too often though. It could be the cause of your lag. The code sample I have provided should only run the code you need when the mouse changes direction on the y axis.

var previous_y_direction = -1 #up = -1, down = +1
func _input(event):
	if event is InputEventMouseMotion:
		if event.relative.y > 0:
			if previous_y_direction == -1:
				previous_y_direction = 1
				_play(previous_y_direction)
		elif event.relative.y < 0:
			if previous_y_direction == 1:
				previous_y_direction = -1
				_play(previous_y_direction)

func _play(direction : int):
	if direction == -1:
		pass # play up animation here
	elif direction == 1:
		pass # play down animation here

Huge Thanks! I haven’t tried it yet but, is it also will be okay to add movement codes into _input(event) function?

Example, if the mouse moves up, the Player node will also move and playing up animation at the same time. same thing to other directions. also the Player will move a lot while playing the game.
so is it a good idea? or should i use another function like _physics_process(delta) or _process(delta) for the movements to avoid overflow?

Si Disap | 2022-04-27 13:42

The input function gets called very rapidly so it would be best to set a velocity variable in the input function and move the character in the physics process. physics process runs at the games framerate while the process function runs as fast as possible.

This will move your character when the mouse is moving but it isn’t tied to your mouse speed

var velocity = Vector2()
const speed = 20
var previous_y_direction = -1 #up = -1, down = +1
func _input(event):
	if event is InputEventMouseMotion:
		if event.relative.y > 0:
			velocity.y += speed
			if previous_y_direction == -1:
				previous_y_direction = 1
				_play(previous_y_direction)
		elif event.relative.y < 0:
			velocity.y -= speed
			if previous_y_direction == 1:
				previous_y_direction = -1
				_play(previous_y_direction)

func _physics_process(delta):
	move_and_slide(velocity) # move your character
	velocity.y *= 0.8 #Inertia

This will make the character match your mouse speed but it’s location is detached

var velocity = Vector2(0.0, 0.0)
var previous_y_direction = -1 #up = -1, down = +1
func _input(event):
	if event is InputEventMouseMotion:
		velocity.y += event.relative.y * Engine.get_iterations_per_second()
		if event.relative.y > 0:
			if previous_y_direction == -1:
				previous_y_direction = 1
				_play(previous_y_direction)
		elif event.relative.y < 0:
			
			if previous_y_direction == 1:
				previous_y_direction = -1
				_play(previous_y_direction)

func _physics_process(delta):
	move_and_slide(velocity) # move your character
	velocity.y = 0 #reset velocity

Snudgley | 2022-04-30 03:17

Okay! Thank you so much! this helps me a lot!

Si Disap | 2022-04-30 09:55