0 votes

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

Godot version 3.2.3.stable.official
in Engine by (141 points)

2 Answers

0 votes

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

by (160 points)

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

+1 vote

Hi!
So you are facing a common misconception about how addforce works: it basically adds a continuous force until removed. To apply an instantaneous force that then stops you need to switch from addforce 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,

by (99 points)

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

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

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)

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

i use rigid mode

Try changing it to character mode since that mode prevents rotation

now he not move

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.