# Simple Jumping Animation

Attention Topic was automatically imported from the old Question2Answer platform.

I made a jumping animation in animation player, its made out of 6 frames, and when i jump the animation is not the one i made in animation player.
here is my code:

``````if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = JUMP_HEIGHT
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
else:
if motion.y < 0:
\$AnimationPlayer.play("Jump2")
else:
\$AnimationPlayer.play("Fall")
if friction == true:
motion.x = lerp(motion.x, 0, 0.05)

motion = move_and_slide(motion, UP)
``````

now i understand the Fall animation will cancel it, but its the same if i remove the fall animation, the jump animation simply doesent get played.

here’s the full code
its from a tutorial:

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
const MAX_SPEED = 120
const JUMP_HEIGHT = -450

var motion = Vector2()

func _physics_process(delta):
motion.y += GRAVITY
var friction = false

``````if Input.is_action_pressed("move_right"):
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
\$Sprite.flip_h = false
\$AnimationPlayer.play("Walk")
elif Input.is_action_pressed("move_left"):
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
\$Sprite.flip_h = true
\$AnimationPlayer.play("Walk")
else:
\$AnimationPlayer.play("Idle")
friction = true

if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = JUMP_HEIGHT
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
else:
if motion.y < 0:
\$AnimationPlayer.play("Jump2")
if friction == true:
motion.x = lerp(motion.x, 0, 0.05)

motion = move_and_slide(motion, UP)

pass
``````

it actually worked with an animatedSprite2d but with a different sprite and with animation player it doesent

Gouki | 2020-04-19 20:21

worked on my code, still not working, any help is appreaciated:
extends KinematicBody2D

enum {IDLE, WALK, JUMP}
const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
const MAX_SPEED = 120
const JUMP_HEIGHT = -450
var state
var anim
var new_anim
var motion = Vector2()

change_state(IDLE)

func change_state(new_state):
state = new_state
match state:
IDLE:
new_anim = ‘Idle’
WALK:
new_anim = ‘Walk’
JUMP:
new_anim =‘Jump2’

func _physics_process(delta):
motion.y += GRAVITY
var friction = false

``````if Input.is_action_pressed("move_right"):
change_state (WALK)
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
\$Sprite.flip_h = false
if new_anim != anim:
anim = new_anim
\$AnimationPlayer.play(anim)
elif Input.is_action_pressed("move_left"):
change_state (WALK)
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
\$Sprite.flip_h = true
if new_anim != anim:
anim = new_anim
\$AnimationPlayer.play(anim)
else:
change_state (IDLE)
if new_anim != anim:
anim = new_anim
\$AnimationPlayer.play(anim)
friction = true

if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = JUMP_HEIGHT
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
else:
if motion.y < 0:
change_state (JUMP)
if new_anim != anim:
anim = new_anim
\$AnimationPlayer.play(anim)
if state == JUMP and is_on_floor():
change_state (IDLE)
if friction == true:
motion.x = lerp(motion.x, 0, 0.05)

motion = move_and_slide(motion, UP)

pass
``````

Gouki | 2020-04-20 13:02

tried this too :

``````		if \$AnimationPlayer.current_animation != "Jump2":
\$AnimationPlayer.play("Jump2")
``````

still same

Gouki | 2020-04-20 13:18

The other animations are cancelling the jump animation out. Put everything in the left, right and idle code except for the `motion.x = ...` lines under the `if is_on_floor():` condition, then it will only play those animations if `is_on_floor()` is true.

Thank you, working perfectly now, i feel stupid now…
but i dont understand why leave the motion.x above the if is_on_floor(), then the player doesent move at all, but when i move those codes too it works fine…
EDIT:
the jump is fine now, but i cant move in the air when i move the motions too,
but when i dont move the motions my player doesent move left or right anymore… what am i missing here

Gouki | 2020-04-20 19:24

Did you move them over like this?

``````if Input.is_action_pressed("move_right"):
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
\$Sprite.flip_h = false
...and the rest

if is_on_floor()
if Input.is_action_pressed("move_right"):
change_state (WALK)
if new_anim != anim:
anim = new_anim
\$AnimationPlayer.play(anim)
...and the rest
``````

Magso | 2020-04-20 19:49

Ok so i made a function

`````` func play_anim(anim_name):
if anim_player.is_playing() and anim_player.current_animation == anim_name:
return
``````

figured im gonna need new animations for WalkLeft and WalkRight so it doesent mess it up.
but now everything is working except the WalkRight animation doesent play, the PC moves to the right tho. idk whats wrong… here is what ive done so far:

``````extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
const MAX_SPEED = 120
const JUMP_HEIGHT = -450
var motion = Vector2()

if Input.is_action_pressed("move_right"):
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
\$Sprite.flip_h = false
elif Input.is_action_pressed("move_left"):
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
\$Sprite.flip_h = true
if is_on_floor():
friction = true
if Input.is_action_pressed("move_right"):
anim_player.play('WalkRight')
if Input.is_action_pressed("move_left"):
anim_player.play('WalkLeft')
else:
anim_player.play('Idle')
if Input.is_action_just_pressed("jump"):
motion.y = JUMP_HEIGHT
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
else:
if motion.y < 0:
anim_player.play('Jump')
if friction == true:
motion.x = lerp(motion.x, 0, 0.05)

motion = move_and_slide(motion, UP)
``````

Now im not sure if my function is messing it up, or the \$Sprite.flip_h, because i flipped it already in the AnimationPlayer( i keyed a flip_h ), but when i remove the flip still not working, but the jump is finally fixed !

Gouki | 2020-04-20 21:21

Right isn’t working because the code is doing this:

``````if right:
plays WalkRight
if left:
not true so it doesn't play
else:
takes over WalkRight and plays Idle
``````

left needs to be `elif`

Magso | 2020-04-20 21:58

omg thank you so much buddy, i dont know how didnt i see this,
back to basics i go!

Gouki | 2020-04-20 22:01