Why doesn't my code work?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sunrise games

I am trying to create a platformer game. I am still stuck trying get my character to move and animate. I have made a run animation and an idle animation, and I copied the code from this video: https://www.youtube.com/watch?v=mHeh-8xZvBo&t=214s. However, when I run the game my character doesn’t do anything. I don’t have any errors or warnings. I am also worried that there is something wrong with my computer or the game editor. It never worked right, but the character used to move or animate. Now, it does nothing. Here is my code:

extends KinematicBody2D

const UP_DIRECTION := Vector2.UP

export var speed := 1500
export var jump_strength := 2000
export var maximum_jumps := 1
export var gravity := 4500

var velocity = Vector2.ZERO

onready var pivot: Node2D = get_node("/root/Player/Body")
onready var animationplayer: AnimationPlayer = get_node("/root/Player/AnimationPlayer")
onready var start_scale: Vector2 = pivot.scale 

func _physics_process(delta: float) -> void:
	var horizontal_direction = (
		Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	)

	velocity.x = horizontal_direction * speed
	velocity.y += gravity * delta


	var _is_falling = velocity.y > 0.0 and not is_on_floor()
	var is_jumping = Input.is_action_just_pressed("jump") and is_on_floor()
	var is_jump_cancelled = Input.is_action_just_released("jump") and velocity.y < 0.0
	var is_idling = is_on_floor() and is_zero_approx(velocity.x)
	var is_running = is_on_floor() and not is_zero_approx(velocity.x)

	if is_jumping:
		velocity.y = -jump_strength
	elif is_jump_cancelled:
		velocity.y = 0.0

	velocity = move_and_slide(velocity, UP_DIRECTION)

	if not is_zero_approx(velocity.x):
		pivot.scale.x = sign(velocity.x) * start_scale.x

	if is_running:
		animationplayer.play("run_right")
	elif is_idling:
		animationplayer.play("Idle")

Can anyone tell me what I’m doing wrong?

Edited to fix code formatting. In future posts, use the {} button to properly format code blocks for the forum.

jgodfrey | 2023-02-02 21:52

This is very different to the way I code movement in my games but I cannot see anything specifically wrong. In your input mapping have you got keys assigned too the actions “move_right” and “move_left”? Obviously nothing would work if not. You can check this by going to your project>project settings>Input map in the bar at the top of godot.

Gluon | 2023-02-02 23:13

I checked my input keys, but everything is correct. You mentioned that this is not how you normally code movement in your games. How do you normally do it?

sunrise games | 2023-02-07 20:22

It’s really hard to effectively debug that from a forum post. If I were you, I’d drop some break points at critical locations in physics_process() and start examining some of the local variables. Something isn’t as you expect it to be. Spotting it via the debug inspector (or, by adding some simple print statements) should be pretty straight forward…

jgodfrey | 2023-02-07 21:44

Are you sure you are pressing the correct keys? It’s really stupid but I have had this problem in the past.

exuin | 2023-02-28 05:58