0 votes

My player character is a RigidBody2D, and I am trying to make him move by overriding the 'integrateforces' 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.

in Engine by (60 points)

2 Answers

0 votes

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.

by (1,328 points)

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.

0 votes

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.

by (60 points)
edited by
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 [email protected] with your username.