Jumping jack, animations not playing

I am following the godot mobile tutorial and when it comes to the player jump section I can’t seem to make the animation for falling play

extends CharacterBody2D
class_name Player

@onready var animation_player: AnimationPlayer = $AnimationPlayer

const SPEED = 300.0
const JUMP_VELOCITY = -800.0
const margin = 20

func jump():
	if velocity.y >0:
		velocity.y = JUMP_VELOCITY

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# 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("move_left", "move_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	var viewport_width := get_viewport_rect().size.x
	if global_position.x > viewport_width + margin:
		global_position.x = -margin
	elif global_position.x < -margin:
		global_position.x = viewport_width + margin
func _process(delta: float) -> void:
	if velocity.y >0 and animation_player.current_animation !="falling":
		animation_player.play("falling")
		print("fall")
	elif velocity.y <0 and animation_player.current_animation !="jump":
		animation_player.play("jump")
		print("jump")
[gd_scene load_steps=8 format=3 uid="uid://mathntpk7xmn"]

[ext_resource type="Texture2D" uid="uid://cegs3jmqlbimw" path="res://player/Skin1Idle.png" id="1_ygxgv"]
[ext_resource type="Script" path="res://player/player.gd" id="1_yiybi"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_0fhtc"]
size = Vector2(34, 20)

[sub_resource type="Animation" id="Animation_52csj"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_ygxgv")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, -46)]
}

[sub_resource type="Animation" id="Animation_xuk5l"]
resource_name = "jump"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_ygxgv")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, -46)]
}

[sub_resource type="Animation" id="Animation_g3t75"]
resource_name = "falling"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_ygxgv")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, -46)]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_o88vj"]
_data = {
"RESET": SubResource("Animation_52csj"),
"falling": SubResource("Animation_g3t75"),
"jump": SubResource("Animation_xuk5l")
}

[node name="Player" type="CharacterBody2D"]
collision_mask = 2
script = ExtResource("1_yiybi")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -10)
shape = SubResource("RectangleShape2D_0fhtc")

[node name="Sprite2D" type="Sprite2D" parent="."]
z_index = 100
position = Vector2(0, -46)
scale = Vector2(0.35, 0.35)
texture = ExtResource("1_ygxgv")

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_o88vj")
}
```

What I see because in the code it is not defined when it should fall. I would interfere with an additional IF statement under if not iis_on_floor, the case of animation.

1 Like

@Oldeveu Not a bad thought since that’s how Martian Mike works, but this game operates in a different way. is_on_floor() does not come into play at all because there is no run cycle; since there is other code to facilitate the jump itself, the animation playing can be based on velocity alone =)

I don’t see any issues with this at first glance; I plugged your code into the repo project and that worked fine too (after renaming animation_player to match what was in the project). Since you posted the .tscn raw code in here as well, I was also able to confirm that there is no mismatch in the names of your animations.

If you aren’t getting any error messages in the debugger, I think the easiest way to get this fixed at this point is if you upload your project so I can take a closer look: project upload.

The jump method is called from outside and that is working

I had a funny feeling I’d want to skip a few steps and just look directly at this project. As simple as the problem was, I don’t think I would’ve thought to ask about this, so my hunch was right and we saved some time!

Your code is good, but your falling and jump animations are identical. After setting the Skin1Jump image as the Sprite2D’s texture and keying that into the jump animation, everything was working as expected. If you need me to go into more detail about that, let me know; otherwise, have fun with the project!

Ok It is just me or is the ui around animations in Godot a little bit counterintuitive?

Animation work in general takes some getting used to. I’ve actually found that Godot’s UI for it is one of the better ones, but I didn’t grasp it right away either. If this is your first project in Godot, it doesn’t help that these “animations” are only 1 frame; it would be worth your while to explore the dock a bit more and just experiment with things to see what can be done =)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms