0 votes

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.

Godot version 3.4.4
in Engine by (17 points)
edited by

1 Answer

+1 vote
Best answer

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
by (86 points)
selected by

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 physicsprocess(delta) or _process(delta) for the movements to avoid overflow?

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.