RigidBody2D can’t be set via
position, it interferes with the physics simulation.
From the docs for RigidBody2D:
You should not change a RigidBody2D’s position or linear_velocity every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.
And the description for _integrate_forces:
Allows you to read and safely modify the simulation state for the object. Use this instead of _physics_process if you need to directly change the body’s position or other physics properties.
For more examples and details, see this tutorial:
fixed_process() was the name in 2.1, the function is named
I don’t want to change RigidBody2D’s position every frame, i want to change it only when gamer presses R key.however,if set_position not works, is there any other way to change rigidbody’s position? i tried putting function into _integrate_forces() or _process(), or putting rigidbody into sleep for one frame, none of them worked. still stuck with this problem.
lostact | 2018-01-31 22:25
As in the link, you have to use
_integrate_forces() and change the position using the
kidscancode | 2018-01-31 22:35
thank you, it solved the problem.
lostact | 2018-01-31 22:41
It can be done in
_physics_process by getting the body state via Physics2DServer (to change the transform) but not sure how reliable will be the result compared to the custom integrator.
This is a misconception. Using
_integrate_forces() just gives you safe access to the physics state. It has nothing directly to do with custom integration, which you can enable and also would be used in that function.
kidscancode | 2018-02-01 03:53
I see, thanks!
I always use it custom physics only.
What about instancing the node. Then you can set the position of the rigidbody just before you add the child. I think that should also do it.
Enquest | 2018-02-28 19:56