I'm using a script to control my player by pointing and clicking with the mouse and its walking animation keeps playing after it has reached its location when I want it to play its idle animation. I tried to tell the player to play the idle whenever the velocity is 0, but it seems that the velocity snaps to its value whenever the mouse is clicked, rather than going back down to 0 when it reached it's location like I thought it would (
.
Games with traditional arrow key control typically just tell the character to play the idle animation when no arrow keys are being pressed. Is there something similar to this in point and click control? Here's the script:
extends KinematicBody2D
var speed = Vector2(64 * 3, 64 * 3) # 64 pixels per second
var last_mouse_pos = null
var target = Vector2()
onready var animationPlayer = $AnimationPlayer
onready var sprite = $Sprite
func _input(event):
if event.is_action_pressed('click'):
last_mouse_pos = get_global_mouse_position()
# warning-ignore:unused_argument
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("back_walk")
elif velocity.x < velocity.y && velocity.x > -velocity.y:
animationPlayer.play("front_walk")
else:
animationPlayer.play("side_walk")
sprite.flip_h = velocity.x < 0
if velocity.x == 0 && velocity.y == 0:
animationPlayer.play("side_idle")
print("velocity.x equals", velocity.x)
print("velocity.y equals", velocity.y)