I was attempting to change the gravity scale of a RigidBody2D in GD script, and was/am confused as to why my program was behaving as it did.
I have a single RigidBody2D, with a UI-set gravity scale of 0. I want to change the gravity scale to 3 when a user clicked the up arrow, but found I could not get it to work as expected. Code progressed as seen below.
To start, I simply changed the gravity scale in _fixed_process regardless of input, which worked as expected.
extends RigidBody2D
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
set_gravity_scale(3)
When I modified the code to include checking for the keypress, the body would not begin to move. I checked in the debugger, and the set_gravity_scale(3) line was indeed being run.
func _fixed_process(delta):
if Input.is_action_pressed("ui_up"):
set_gravity_scale(3)
Clutching at straws, I tried modifying the position in the if-block as well. Surprisingly, adding this caused the gravity scale change to take effect.
func _fixed_process(delta):
if Input.is_action_pressed("ui_up"):
set_gravity_scale(3)
var vec = get_pos()
vec.y += 0.01
set_pos(vec)
Why does the original (no input checked) case work, while the case waiting for the input does not? Why does changing the position cause the gravity change to take effect in the 3rd example?
#physics #rigidbody2d #2d