Problem with Jump animation, or either input problem.. Depending on placement of input.

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

I am a complete beginner ( few days into code ) and i’m stuck for a couple of days now.
The issue = my character doesnt stop moving on X when i hold jump after a walking jump. Like it’s freaking out over double inputs.

I can solve it my placing my jump script seperately in input_event instead of _physics_process(delta). But then my jumping animation doesnt work, but the running animation does.

I tried putting everything in input_event but then it’s stuck all completely.
I’m missing something basic here but i cant put my finger on it.

Script:

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var on_ground = false

func jump():
velocity.y = -JUMP_POWER

func _input(event):
if event.is_action_pressed(“ui_up”):
if is_on_floor():
velocity.y = JUMP_POWER
$AnimatedSprite.play(“jump”)

func _physics_process(_delta):

if Input.is_action_pressed("ui_right"):
	velocity.x = SPEED
	if is_on_floor():
		$AnimatedSprite.play("walking")
	$AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
	velocity.x = -SPEED
	if is_on_floor():
		$AnimatedSprite.play("walking")
	$AnimatedSprite.flip_h = true 




	
else:
	velocity.x = 0                # Stops the left/right movement on
	if is_on_floor():
			$AnimatedSprite.play("idle")

velocity = move_and_slide(velocity, FLOOR)
velocity.y += GRAVITY

if is_on_floor():
	on_ground = true
	
else:
	on_ground = false
:bust_in_silhouette: Reply From: FortunePilot

This might not be the solution to your problem, but you might have to look at how you process your input. There are TWO different input checks, one of which checks for continuous input, and one for single-hit input:

Continuous: Input.is_action_pressed("input").
Single-hit: Input.is_action_just_pressed("input").

If you want your player to jump, use the single-hit method.

An example:

# Prints "Pressed!" every single frame until the button is released.
if Input.is_action_pressed("input"):
    print("Pressed!")

# Prints "Pressed!" once until the button is released and pressed again.
if Input.is_action_just_pressed("input")
    print("Pressed!")

Thanks for the quick response, just_pressed for walking left/right moves 1 frame untill release (like the example shows) , it makes sense for jump, so i tried and i dont notice anything different. Probably because i have Gravity setup so it wont keep raising Y on 1 press. It’s really weird… i feel like the code is decent but when i add jump to the script its get messy.

Nickster076 | 2020-04-23 12:34

If I understand correctly, when you are moving left/right, and you press jump, your character doesn’t stop moving left/right?
From looking at your code, begin by putting every input action into the input function. Even though this doesn’t work yet, this is best practice, and will save you time organizing your inputs later on. :slight_smile:

FortunePilot | 2020-04-23 12:45

:bust_in_silhouette: Reply From: njamster

The issue = my character doesnt stop moving on X when i hold jump after a walking jump. Like it’s freaking out over double inputs.

As you haven’t provided your code for that, it’s impossible to point out what you did wrong! :frowning: Furthermore it would be super helpful if you would make sure your code is properly formatted here. If you notice that something is off, you can edit any of your posts by clicking on the three dots in the lower right corner and selecting “Edit”.

I can solve it my placing my jump script seperately in inputevent instead of _physicsprocess(delta). But then my jumping animation doesnt work, but the running animation does.

_input is called before _physics_process. If you press both ui_up and ui_left / ui_right during the same frame, then you will set the animation to “jump” first and “walking” directly after. You simply cannot play multiple animations using the same AnimatedSprite-node! However, you should be able to check if there’s already an animation playing using the AnimatedSprite’s is_playing-method.