0 votes

I am making a action-puzzle game in which two kinematicBody2Ds are
controlled at the same time and they move in the opposite direction.
If I press left key one of them will move in left but the other one
will in the right.

They can't collide with each other as they are in different collision layers. When I run it after sometime both the bodies stop moving but the game still is running. Here's the code of both the bodies

    extends ACTOR_SCRIPT#Boy Script

func _physics_process(delta):#CHECK EVENTS BELOW EVERY TICK(SECOND)
#gravity-
    velocity.y += gravity
#input-
    if Input.is_action_pressed("move_right"):
        velocity.x = walk_speed
    elif Input.is_action_pressed("move_left"):
        velocity.x = -walk_speed
    else:
        velocity.x = 0
    if Input.is_action_pressed("jump") and is_on_floor():
        velocity. y = -jump_force
    else:
        velocity.y += gravity
#movement-
    move_and_slide(velocity, Vector2.UP)

Movement script for kinematicbody 2D(A)

    extends ACTOR_SCRIPT


func _physics_process(delta):#CHECK EVENTS BELOW EVERY TICK(SECOND)
#gravity-
    velocity.y += gravity
#input-
    if Input.is_action_pressed("move_right"):
        velocity.x = -walk_speed
    elif Input.is_action_pressed("move_left"):
        velocity.x = walk_speed
    else:
        velocity.x = 0
    if Input.is_action_pressed("jump") and is_on_floor():
        velocity. y = -jump_force
    else:
        velocity.y += gravity
#movement-
    move_and_slide(velocity, Vector2.UP)

Movement script for kinematicbody 2D(B)


I just don't understand what is happening ,plz help me out.

in Engine by (18 points)

I don't know how can I THANK YOU. My game now works perfectly and I can move forward now. I never knew that those errors mattered so much. I will now see how can I fix other errors, so that they don't give problem later.

1 Answer

0 votes

Your problem is that you do not reset gravity to zero while player is on ground. This can be compared to adding 1kg each second. After few seconds you won't be able to move because of the weight. You need to add

if is_on_floor():
  velocity.y = 0

Multiplication by * delta does not fix problem, if you leave your game for 1 minute you wont be able to move again. You need to reset gravity while player is on floor. Also decrease your gravity by 50-100 times. Moreover, you add gravity twice. First time in #gravity section and second time at the end of the #input section.

Messages you saw is not an errors, but rather warnings. They indicate that you maybe did some mistake. In this case this is not the case, and you can simply add _ before delta (so it would be _delta) to suppress warning.

by (1,462 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 Frequently asked questions and 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.