0 votes

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 inputevent instead of _physicsprocess(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 JUMPPOWER = -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
actionpressed("uiup"):
if isonfloor():
velocity.y = JUMP_POWER
$AnimatedSprite.play("jump")

func physicsprocess(_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
in Engine by (14 points)

2 Answers

0 votes

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!")
by (53 points)

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.

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. :)

+1 vote

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! :( 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.

by (10,327 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.