Can I move a rigidbody with code instead of using a kinematic body?

ArnavArnav Posts: 3Member

Hello everyone! I am new to Godot. I have checked out the documentation for the physics bodies and it says that I can move rigidbody when it is in static or kinematic mode. The reason I want to use a rigid body is because I dont want to have to code all the movements with every collision that takes place. I first used a kinematic body with the move function, which worked well; however, I also want it to obey other physics too (colliding with other rigid bodies).

So could anyone please help me to somehow "move" the rigid body in the same way as the move function? For extra info, I am moving the body based on a swipe gesture, along the x axis.

Thanks everyone :)

Answers

  • TassuPTassuP Posts: 3Member

    Yes, you can use set_linear_velocity instead of move. Works very similarly. Here's an example:
    Kinematic:

    if(Input.is_key_pressed(KEY_RIGHT)):
        move(Vector3(1,0,0)*delta)
    if(Input.is_key_pressed(KEY_LEFT)):
        move(Vector3(-1,0,0)*delta)

    RigidBody in Rigid mode:
    set_linear_velocity(Vector3(0,0,0))
    if(Input.is_key_pressed(KEY_RIGHT)):
        set_linear_velocity(Vector3(1,0,0))
    if(Input.is_key_pressed(KEY_LEFT)):
        set_linear_velocity(Vector3(-1,0,0))
Sign In or Register to comment.