0 votes

hello i am trying to make a character using a ridgidbody2d and i made this script

 extends RigidBody2D

func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if Input.is_action_just_pressed("ui_up"):
        add_force(Vector2(1 , 1), Vector2(0,-14000))
    elif Input.is_action_pressed("ui_right"):
        add_force(Vector2(1 , 1), Vector2(50,00))   
    elif Input.is_action_pressed("ui_left"):
        add_force(Vector2(1 , 1), Vector2(-50,0))
    else: 
        set_applied_force(Vector2(0 , 0))


    if get_applied_force() > Vector2(1000, 0):
        set_applied_force(Vector2(1000, 0 ))
    if get_applied_force()  <= Vector2(-1000, 0):
        set_applied_force(Vector2(-1000, 0 ))
        pass

but when i try to jump when i am moving it dont work

thank you in advance for your help

in Engine by (48 points)

1 Answer

0 votes
if Input.is_action_just_pressed("ui_up"):
    add_force(Vector2(1 , 1), Vector2(0,-14000))
elif Input.is_action_pressed("ui_right"):

If you pressed the up key then the first condition fires and the code continues after that if block. The next executed line is if get_applied_force() > Vector2(1000, 0):

Change elif Input.is_action_pressed("ui_right"): to if Input.is_action_pressed("ui_right"): and you will be able to move and jump at the same time. But note that the logic of your if statement has changed because theelse clause no longer cares about the state of the "ui_up" input.

As a side note, consider making your character a KinematicBody2D node.

by (3,375 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.