Point and click, how do I stop the animation once reached mouse-point?

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

I have been using GMS for a while, but am making the switch to Godot. I have been researching, but can’t find much, or am not understanding. If you have the time, please assist me with this, thank you.


player Script:

extends KinematicBody2D

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

func _input(event):
   if event.is_action_pressed("game_move"):
    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 && velocity.x < -velocity.y:
                    $AnimationPlayer.play("up")
            elif velocity.x < velocity.y && velocity.x > -velocity.y:
                    $AnimationPlayer.play("down")
            elif velocity.x > 1:
                    $AnimationPlayer.play("right")
            else:
                    $AnimationPlayer.play("left")

Thank you again for your assistance and knowledge.
- Server

Do you know how the code here works, and how it stops moving when below a certain distance? If so, it is only a minor change to call a method on the $AnimationPlayer to play an “idle” animation (or stop).

spaceyjase | 2023-02-21 10:59

:bust_in_silhouette: Reply From: Wakatta

That animation condition is usually reserved for the else block and you will need to specifically define your left condition.

#Just an example not accurate
elif velocity.x > 1:
    $AnimationPlayer.play("right")
elif -velocity.x < 1:
    $AnimationPlayer.play("left")
else:
    $AnimationPlayer.stop()

What is the best way to stop the animation on a specific frame in the direction player is last facing? Once at position, he seems to just finish the animation (walks in place) for two frames, then stops at a walking frame instead of the middle idle frame.

Thank you so much for your assistance so far :slight_smile:

Server | 2023-02-21 20:52