I'm trying to add a force while also having a velocity next to it, since I want to make the player move left and right with the velocity and jump with the force.
This is my code:

extends RigidBody2D

func _process(delta):
	var velocity = Vector2.ZERO
	if Input.is_action_just_pressed("jump"):
		apply_force(Vector2(0, -30000))
	if Input.is_action_pressed("left"):
		velocity -= 1
	if Input.is_action_pressed("right"):
		velocity += 1

The game itself works as I intended, including the jumping, but if I press left or right, this error message will appear:

Scene setup (if it helps):

How do I make the character move left and right, and make him jump too?

  • VoxelChicken You are setting the velocity of a RigidBody2D every frame, which according to the docs, is undesired:

    Note: You should not change a RigidBody2D's position or linear_velocity every frame or even very often. If you need to directly affect the body's state, use _integrate_forces, which allows you to directly access the physics state.

    so you can use the _integrate_forces function, but I hope someone posts how it works, I just don't know.
    According to the docs, it goes like this:

    void _integrate_forces ( PhysicsDirectBodyState2D state ) virtual

    Allows you to read and safely modify the simulation state for the object. Use this instead of Node._physics_process if you need to directly change the body's position or other physics properties. By default, it works in addition to the usual physics behavior, but custom_integrator allows you to disable the default behavior and write custom force integration for a body.

    • Now some thoughts, why are you using a RigidBody2D as your player? there is nothing wrong with that if you need some behavior not available in the other node type, but, if you want full control of how your player behaves, I think it is more appropriate to have it be a CharacterBody2D.
    • If you use Godot 4.x (according to your tags), CharacterBody2D script even comes with its own boilerplate that you can modify the way want. It has the code for jumping and moving, with inputs handled, and comments with best practices and how it works.
    • CharacterBody2D movement is predictable, just set the velocity, and call move_and_slide() or move_and_collide(), every _physics_process(delta).
    • That allows it (the character/player) to interact with physics around it, like moving on a StaticBody2D ground and not pass through walls, while also having more freedom with how it moves.

    I hope that was insightful 😃

    Edit: typos

You cannot add a integer number to a vector. What you can do is:
velocity -= Vector2(1,0)
velocity += Vector2(1,0)

    klaas
    Thank you for your answer!
    It's the same as last time, just with no error messages if I press left or right. The jumping works.

    If there's something wrong with the floor, then here is the floor's StaticBody2D:

    And also this is the newly edited code:

    extends RigidBody2D
    
    func _process(delta):
    	var velocity = Vector2.ZERO
    	if Input.is_action_just_pressed("jump"):
    		apply_force(Vector2(0, -30000))
    	if Input.is_action_pressed("left"):
    		velocity -= Vector2(100, 0)
    	if Input.is_action_pressed("right"):
    		velocity += Vector2(100, 0)

      VoxelChicken You are setting the velocity of a RigidBody2D every frame, which according to the docs, is undesired:

      Note: You should not change a RigidBody2D's position or linear_velocity every frame or even very often. If you need to directly affect the body's state, use _integrate_forces, which allows you to directly access the physics state.

      so you can use the _integrate_forces function, but I hope someone posts how it works, I just don't know.
      According to the docs, it goes like this:

      void _integrate_forces ( PhysicsDirectBodyState2D state ) virtual

      Allows you to read and safely modify the simulation state for the object. Use this instead of Node._physics_process if you need to directly change the body's position or other physics properties. By default, it works in addition to the usual physics behavior, but custom_integrator allows you to disable the default behavior and write custom force integration for a body.

      • Now some thoughts, why are you using a RigidBody2D as your player? there is nothing wrong with that if you need some behavior not available in the other node type, but, if you want full control of how your player behaves, I think it is more appropriate to have it be a CharacterBody2D.
      • If you use Godot 4.x (according to your tags), CharacterBody2D script even comes with its own boilerplate that you can modify the way want. It has the code for jumping and moving, with inputs handled, and comments with best practices and how it works.
      • CharacterBody2D movement is predictable, just set the velocity, and call move_and_slide() or move_and_collide(), every _physics_process(delta).
      • That allows it (the character/player) to interact with physics around it, like moving on a StaticBody2D ground and not pass through walls, while also having more freedom with how it moves.

      I hope that was insightful 😃

      Edit: typos

        BroTa
        Thank you for your answer!

        I tried the moving and jumping (-> with the CharacterBody2D), yet nothing works. (Also I don't have gravity anymore, since I changed the scene tree. The scene tree is below.) And on top of that, the scene tree has some problems. I have to add two different colliders for the same player. One for the CharacterBody2D and one for the RigidBody2D. Yet, I'm not getting any errors in the debugger.

        This is the scene tree:

        The error reads:

        "
        This node has no shape, so it can't collide or interact with other objects.
        Consider adding a CollisionShape2D or a CollisionPolygon2D as a child to define its shape.
        "

        If I add the CollisionShape2D, that doesn't help either.

          VoxelChicken I tried the moving and jumping (-> with the CharacterBody2D), yet nothing works.

          • Seeing the image in your post, there is no script attached to you player CharacterBody2D. You need movement code to.. move the character around.
          • I am sorry if my previous explanation was vague, but the CharacterBody2D does not move without a script, but when you try to create a CharacterBody2D script, you can choose whether you want it empty, or fill it with basic code that moves the CharacterBody2D.

          VoxelChicken I have to add two different colliders for the same player. One for the CharacterBody2D and one for the RigidBody2D.

          • You don't need the RigidBody2D to be part of your character, unless there is some effect to that I am not aware of.

          • You can think of rigid bodies as game objects that are affected by physics out of the box, while character bodies are game objects that you should control directly through code, but they can also affect and get affected by physics around it. (Out of the box, it can handle collisions when it has a collision child, but no movement without code, though.)

          • With your setup, you have a player, that has some part that is affected by physics. This might give unpredictable behaviours. There is no context, but I believe that 999 times out of 1000, you won't need the rigid body as a child of your character body.

          • Try adding a sprite (godot icon should suffice) and a collision shape to the rigid body that's a child of the player, move the rigid body below its parent in the editor, so collision shapes dont overlap.

          • You can also enable visible collision shapes in the debug menu above.

          • Now run the scene and observe what happens.

          • I didn't try it this way, but I predict you will see the rigid body falling (being affected by gravity)

          • When your movement script works, and move the player around successfully, you will also see the rigid body moving, as it is a child of the player node.

          • tl;dr: you most probably don't need the rigid body to move the player around. you need a script attached to your player with movement code.

          • I hope that was helpful, and feel free to reply if you need anything else.

            BroTa

            Thank you very much for your answer!

            Since today, I figured I could change the project into a different one. But thanks a lot for your time and commitment!