Cant manage to add jump animation

Godot Version

Question

so i forgot to add my jump animation into the script so today i decided to add it into the script wwich already had the defult and running animation in it(they both work fine) but when i add the jump animation script it either stays stuck as the first frame of the jumping animation or removes both animations and its just my character frozen

heres my code

func _physics_process(delta):
# Animations
if (velocity.x > 1 || velocity.x < -1):
sprite2d.animation = “running”
else:
sprite2d.animation = “defult”

# Add the gravity.
if not is_on_floor():
	velocity.y += gravity * delta
sprite2d.animation = "jumping"
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY

please help

Here is the correct codes:

func _physics_process(delta):
    # Animations
    if is_on_floor():
        if (velocity.x > 1 || velocity.x < -1):
            sprite2d.animation = “running”
        else:
            sprite2d.animation = “defult”
    else:
        sprite2d.animation = "jumping"
    # Add the gravity.
    if not is_on_floor():
	    velocity.y += gravity * delta

    # Handle jump.
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	    velocity.y = JUMP_VELOCITY

It will play the jump animation if you are not in floor, also you can add a fall animation then you need to add a timer for the jump animation

2 Likes

also when i added the code it said used tab character indentation instead of space as used before in the file btw heres the entire script im using

extends CharacterBody2D

const SPEED = 270.0
const JUMP_VELOCITY = -700.0
@onready var sprite2d = $Sprite2D

et the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):
# Animations
if is_on_floor():
if (velocity.x > 1 || velocity.x < -1):
sprite2d.animation = running
else:
sprite2d.animation = defult
else:
sprite2d.animation = “jumping”
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
    velocity.y = JUMP_VELOCITY



# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.

var direction = Input.get_axis(“ui_left”, “ui_right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 10)

move_and_slide()


var isleft = velocity.x < 0
sprite2d.flip_h = isleft
1 Like

you got any vids that show u how to do the fall animation and adding the timer? also thank you

If you wait 2 days then I can create a simple and easy tutorial on it, I search it on YouTube but not found a better ones
Note: I will do it with animation tree and animated sprite for smooth animation transition

Example Video:


Not exact that, just the player controller tutorial I will create.

no thank you but i do have a problem when i added the code you gave me it gave me an error that i cant find a way to fix check the other reply i sent you it has my the script with what error im having

Paste code between three ticks, you can do this by clicking the </> button on a new line or pressing ctrl+e then pasting your code. like so

```
type or paste code here
```

An indentation error is probably from directly copying code from the forum or elsewhere, check the affected lines and replace spaces with tabs. chances are your editor shows little ⇥ symbols for tabs and nothing for spaces.

Yeah, do not copy paste the codes, just write like that, the tab mistake because I am typing this in mobile, I will watch computer later

uuuuhh turns out it doesnt play the animation when im not on the floor lol when im on the floor it plays the jump animation and not the idle and animation and it also doesnt flip when go in a diff direction (the running is okay its just the jumping i have difficulty with now) heres the script i didnt copy n paste from you and wrote it all

extends CharacterBody2D

const SPEED = 270.0
const JUMP_VELOCITY = -700.0
@onready var sprite2d = $Sprite2D

et the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):

Animations

if (velocity.x > 1 || velocity.x < -1):
	sprite2d.animation = "running"
else:
	sprite2d.animation = "jumping"

Add the gravity

if not is_on_floor():
	velocity.y += gravity * delta

Handle jump.

if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions. 
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED 
else:
	velocity.x = move_toward(velocity.x, 0, 10)

move_and_slide()


var isleft = velocity.x < 0
sprite2d.flip_h = isleft

Ohh, you not read my previous reply properly, it will be:

if is_on_floor():
    if (velocity.x > 1 || velocity.x < -1):
        sprite2d.animation = “running”
    else:
        sprite2d.animation = “defult”
else:
    sprite2d.animation = "jumping"

If you need far help like how to create fall/jump animation then you can tell me, it is possible with timer

where do i add it or do i need to use it to replace something?

Replace it:

if (velocity.x > 1 || velocity.x < -1):
	sprite2d.animation = "running"
else:
	sprite2d.animation = "jumping"

it says "expected statement, found “else instead” how do i fix this and im sorry if im being annoying

I think you made a mistake with “else”:

if is_on_floor():
    if (velocity.x > 1 || velocity.x < -1):
        sprite2d.animation = “running”
    else: #First else
        sprite2d.animation = “defult”
else: #Second
    sprite2d.animation = "jumping"

Have you write like that? see the tabs

finna write this pray for me🙏

1 Like

…theres nothing wrong with what i wrote i fixed the errors i got after i wrote everything but just one line of code also btw it makes it easier for me if u put like a number before every line of code to see how many tabs i have to put

else: #Second

thats the line of code that has a problem and the error is “expected statement but found “else” instead”

Add a extra tab here:

else: #First else
    sprite2d.animation = "jumping"

Full:

if is_on_floor(): #zero tab
    if (velocity.x > 1 || velocity.x < -1): #1 Tab
        sprite2d.animation = “running” #2
    else: #First else #1
        sprite2d.animation = “defult” #2
else: #Second
    sprite2d.animation = "jumping" #1
1 Like

i did that wich made another error wich i quickly fixed by adding one tab to if(velocity.x > 1 || velocity.x < -1):
sprite2d.animation = “running”

but that didnt solve my current error wich ive been asking for a solution for “expected statement found “else” instead” ive been terrorized by this error everytime i sleep this error shows up in my nightmares

Ohhh, match my code tabs and yours
again, I am showing you with number of tabs count:

func _physical_process(delta):
    if is_on_floor(): #1 Tab
        if (velocity.x > 1 || velocity.x < -1): #2 Tabs
            sprite2d.animation = “running” #3 Tabs
        else: #2 Tabs
            sprite2d.animation = “defult” #3 Tabs
    else: #1 Tabs
        sprite2d.animation = "jumping" #2 Tabs

Also I drew

Looks like you are completely new to programming

1 Like

thank you this worked and luckily and theres no need to add a timer god bless you

1 Like