Why does my game break after sometime of playing it.

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

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.

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.

Noon_Coder | 2021-01-11 07:47

:bust_in_silhouette: Reply From: AlexTheRegent

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.