# Velocity vector turns into (0,0) while sliding, but character still moves

4.2.2

### Question

In my game, my character uses the velocity vector length in order to determine whether to use idle or walk animations. If length is 0 then it’ll use idle animations, else, the walk ones. The thing is, while sliding to some specific surfaces, the vector length decreases to 0, but the character still moves, and that makes my character move but with its idle pose. I don´t understand why this happens or if there is a better way to implement the animations.
I’d really appreciate if someone could help.

I think it is not enough, if possible show the codes

1 Like

Not much more regarding to movement (yeah i’m pretty newbie)

Also, here is a link to a video that shows the issue:

And this is how I implemented animations

Try to use elif like:

``````if look == "right":
#...Codes
elif look == "left":
#...Codes
``````
1 Like

Thanks, but looks like that’s not the problem, still happens

if possible, show full codes

extends CharacterBody2D

var direction:Vector2 = Vector2(0,0)
@export var speed:int = 300
var look:String

\$AnimatedSprite2D.play(“down_idle”)

func _physics_process(delta):

``````#control

direction = Input.get_vector("left","right","up","down")

#movement
velocity = direction * speed
move_and_slide()

#look direction
if direction.x > 0:
look = "right"
if direction.x < 0:
look = "left"
if direction.y < 0 and direction.x == 0:
look = "up"
if direction.y > 0 and direction.x == 0:
look = "down"

#animations
if look == "right":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("right_walk")
else:
\$AnimatedSprite2D.play("right_idle")
elif look == "up":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("up_walk")
else:
\$AnimatedSprite2D.play("up_idle")
elif look == "down":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("down_walk")
else:
\$AnimatedSprite2D.play("down_idle")

if look == "left":
\$AnimatedSprite2D.flip_h = true
if not velocity.length() == 0:
\$AnimatedSprite2D.play("right_walk")
else:
\$AnimatedSprite2D.play("right_idle")
else:
\$AnimatedSprite2D.flip_h = false
``````

(Sorry I can’t send more than 1 screenshot)

Do like that:

``````#animations
if look == "right":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("right_walk")
else:
\$AnimatedSprite2D.play("right_idle")
elif look == "up":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("up_walk")
else:
\$AnimatedSprite2D.play("up_idle")
elif look == "down":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("down_walk")
else:
\$AnimatedSprite2D.play("down_idle")
elif look == "left":
if not velocity.length() == 0:
\$AnimatedSprite2D.play("right_walk")
else:
\$AnimatedSprite2D.play("right_idle")

\$AnimatedSprite2D.flip_h = true if look == "left" else false

``````

Doesn’t work, but at least the code is more organized now haha. I think it has something to do with the velocity vector, its values do not correspond to what’s happening in game. But thanks for the help so far!

Yeah, something is wrong with the velocity, I watched the video again😅

1 Like