Lets say I have two objects and one is a Rigidbody2D. For the Rigidbody2D I attached a script which contains a function get_moving(){apply_central_impulse(Vector2(1,0)*100}. I also attached a script to the other one which calls get_moving() of the rigidbody2d once. It is called in _process(). However, the rigidbody does not move. It does move if I directly do a apply_central_impulse with the rigidbody reference eg rigidbody2d.apply_central_impulse, but does not have any effect if I call rigidbody2d.get_moving(). I wonder why this happens????
Calling "apply_central_impulse()" does not have any effect on Rigidbody2D
Okay I just figured out what I have been doing wrong. Sorry that I actually add a velocity control for the rigidbody2d. If the speed of the rigidbody2d is outside a certain range, the script will, in _process()
, make the speed inside the range and calculate the vector required using normalized()
function. It seems the apply_central_impulse()
has one frame delay (or other reasons) so the _process()
is called before physics calculation where velocity is added to the object. Thus the speed control part, which directly reset the velocity using normalized()
(which results in a zero vector because it is originally still), removes the effect of the apply_central_impulse()
. Nothing happens as a result.
Actually I don't know that Vector2().normalized() gives Vector2(). That's weird in the world of maths xd
@gkl223 said: It seems the
apply_central_impulse()
has one frame delay (or other reasons) so the_process()
is called before physics calculation where velocity is added to the object.
As I know Godot processing works as follows:
_physics_process() #Game specific logic connected with physics
(...) #Internal physics calculations, collisions etc.
_process() #Other logic not connected with physics
Maybe you should try call your method in _physics_process()
instead?
@gkl223 said: Actually I don't know that Vector2().normalized() gives Vector2(). That's weird in the world of maths xd
You mean Vector2(0.0, 0.0).normalized() return Vector2(0.0, 0.0)? Normalized vector is done dividing vector by its lenght. In case of Vector2.ZERO it would cause "divide by 0 exception", so it seems resonable it return just the same value, optionally it could assert execution, but would be that better?