How to use '_integrate_forces'?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mudley
:warning: Old Version Published before Godot 3 was released.

My player character is a RigidBody2D, and I am trying to make him move by overriding the ‘_integrate_forces’ function, but its behavior seems really odd.

If I start the game pressing nothing, none of the controls work. But if I start the game with one of them pressed, then it will respond while something is being pressed, after that, nothing works again.

:bust_in_silhouette: Reply From: YeOldeDM

Check out the 2D Platformer demo project (the one with the robot-looking player). That’s a good example of how to use integrated_forces on a player object.

It sounds like your issue is likely in how your code is written. And without being able to see that, it’s anybody’s guess as to what could be going on there.

The simplified code, its very rough, because I am just testing things:

func _integrate_forces(state):
if (Input.is_key_pressed(KEY_W)):
	state.set_linear_velocity(Vector2(cos(angle * (PI / 180)), sin(angle * (PI / 180)) * -speed))
if (Input.is_key_pressed(KEY_S)):
	state.set_linear_velocity(Vector2(cos(angle * (PI / 180)), sin(angle * (PI / 180)) * speed))

And from what I have inspected of the platformer demo, I can’t see what I did wrong.

Mudley | 2017-05-30 19:41

:bust_in_silhouette: Reply From: Mudley

Found the answer, changing the mode to ‘Character’ solved it.

EDIT: Not exactly, as this makes it behave more like a KinematicBody, which defeats the purpose.

EDIT 2: Apparently the problem lied in allowing the body to sleep, toggling it off actually solved it.