Add_central_force pushing rigidbody the wrong way

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alskdjfhg

I have a rigidbody that I move with

if Input.is_action_pressed(“Forward”):
add_central_force(to_global(Vector3(-100,0,0)))
if Input.is_action_pressed(“Left”):
add_central_force(to_global(Vector3(0,0,100)))

etc.

The problem is that when I move it left, right, up, or down it starts moving forward too, even though as far as I can tell there’s no reason for it to

can you supply more code and in “code format” to debug and forward is -x?

klaas | 2020-07-23 11:04

GitHub - frenchma/Game-stuff

this is all the code I’ve got in the file

alskdjfhg | 2020-07-23 18:59

I’ve tested it … no forward movement in my case.
Are your sure that you dont messed up the keybindings?
Maybe you have the forward key bindet to the other directions too!?

klaas | 2020-07-23 19:14

The keybinds are all good, maybe it has something to do with the physics?

I’ve zipped up the game files and stuck that on the github, if anyone wants to take a look

alskdjfhg | 2020-07-23 19:53

:bust_in_silhouette: Reply From: klaas

Ah, now i see.

The problem is the force vector. You have add_central_force in a global direction but you have to remove your global offset (global.origin)

if Input.is_action_pressed("Port"):
	add_central_force(to_global(Vector3(0,0,100))-global_transform.origin)
if Input.is_action_pressed("Starboard"):
	add_central_force(to_global(Vector3(0,0,-100))-global_transform.origin)

this does the trick.