How to stop animation in Click and Move.

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

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")
:bust_in_silhouette: Reply From: Wakatta

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

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

RiqueDiaz | 2021-11-21 21:14

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

Wakatta | 2021-11-22 00:08