make movement using add_force RigidBody

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

i wanna make movement for RigidBody using add_force

extends RigidBody

export var speed := 3.0

    func _physics_process(_delta):
    var velocity := Vector3.ZERO

    if Input.is_action_pressed('up'):
        velocity.z -= speed
    if Input.is_action_pressed('left'):
        velocity.x -= speed
    if Input.is_action_pressed('down'):
        velocity.z += speed
    if Input.is_action_pressed('right'):
        velocity.x += speed

    add_force(velocity.rotated(Vector3.UP, rotation.y), Vector3.ZERO)

I move a little forward and then try to stop this movement by moving backward, but it does not stop.

demonstration: https://drive.google.com/file/d/1JhLlJ2dShVVgr8aqLDEpEP6buxWe54wa/view?usp=sharing

:bust_in_silhouette: Reply From: Spyrex

It’s not the add_force() method, but using set_linear_velocity() it works for me.

I know this, but I need that after stopping the movement, the ball still moves thanks to the application of force

Timofey | 2021-04-10 20:09

:bust_in_silhouette: Reply From: MikeSundaysGameDev

Hi!
So you are facing a common misconception about how add_force works: it basically adds a continuous force until removed. To apply an instantaneous force that then stops you need to switch from add_force to apply_impulse:

extends RigidBody

export var speed := 3.0

func _physics_process(_delta):
var velocity := Vector3.ZERO

if Input.is_action_pressed('up'):
    velocity.z -= speed
if Input.is_action_pressed('left'):
    velocity.x -= speed
if Input.is_action_pressed('down'):
    velocity.z += speed
if Input.is_action_pressed('right'):
    velocity.x += speed

apply_impulse(velocity.rotated(Vector3.UP, rotation.y), Vector3.ZERO)

I hope that it helps,

Btw sorry for the bold and the italic for some reason i didn’t press the bold button, but even so they appeared

MikeSundaysGameDev | 2021-04-10 09:49

thanks, I will try not to forget to try it the next day

Timofey | 2021-04-10 20:11

when driving forward and then backward, it starts spinning to the right. Round
maybe force should be given off-center force?

extends RigidBody

export var speed := 0.3

func _physics_process(_delta):
	var velocity := Vector3.ZERO

	if Input.is_action_pressed('up'):
		velocity.z -= speed
	if Input.is_action_pressed('left'):
		velocity.x -= speed
	if Input.is_action_pressed('down'):
		velocity.z += speed
    if Input.is_action_pressed('right'):
    	velocity.x += speed

	velocity = velocity.rotated(Vector3.UP, rotation.y)

	apply_central_impulse(velocity)

Timofey | 2021-04-11 08:41

Which rigidbody mode are you using? Rigid, Character or Kinematic?

MikeSundaysGameDev | 2021-04-11 08:46

i use rigid mode

Timofey | 2021-04-11 09:55

Try changing it to character mode since that mode prevents rotation

MikeSundaysGameDev | 2021-04-12 21:48

now he not move

Timofey | 2021-04-13 08:25