+1 vote

I'am trying to make a click to move type of movemment, and just for test i added only 2 animations for walking and idle. The wlaking animation works in both right and left direction, but it doesn't stops even when the player is not moving. I think it is not processing when the velocity is 0, because i even use a print code when the velocity in x and y are 0, but it also does nothing when the player stops.

var speed = Vector2(200, 200)
var last_mouse_pos = null

func _input(event):
 if event.is_action_pressed("mouse"):
    last_mouse_pos = get_viewport().get_mouse_position()

func _physics_process(delta):

 if last_mouse_pos:
    var direction_vector = (last_mouse_pos - global_position)

    if direction_vector.length() < 3:
        return

    var velocity = move_and_slide(direction_vector.normalized() * speed)

    if velocity.x && velocity.y > 0:
        $AnimationPlayer.play("frente")
    if velocity.x && velocity.y < 0:
        $AnimationPlayer.play("frente2")
    if velocity.x && velocity.y == 0:
        $AnimationPlayer.play("neutro")
Godot version 3.2.1
in Engine by (30 points)

1 Answer

0 votes

Because of the way floats work which are used by vector2(float, float) you may never get absolute 0 unless you specifically and directly set it

Also to clearify this line

if velocity.x && velocity.y == 0:

# is read as
if velocity.x not 0 and velocity.y is 0
by (5,188 points)

Is there a way i can make the game know when the player isn't moving?

This answer is solely based on the code you've provided

if velocity.x == 0 && velocity.y == 0:
    $AnimationPlayer.play("neutro")

Or

if velocity.is_equal_approx(Vector2(0, 0)):
    $AnimationPlayer.play("neutro")

But since you have this code

if direction_vector.length() < 3:
    return

It may be impossible to achieve

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]tengine.org with your username.