How do I add movement impulse to a character body2d! please help!

Godot Version

4

Question

How do I add movement impulse to a character body2d

character body 2D it is governed by the move_and_slide() or move_and_collide() methods. you can only change the velocity, any “impulse” would have to be manually calculated.
because this node is kinematic, it means it moves by automatically changing its position rather than being moved using physical forces. you decide when it starts moving and when it stops, whereas with a RigidBody, you would set an impulse and the object would stop on its own.
a character body can however stop if it collides with a wall, and move other physics objects.

the editor comes with a built-in script for controlling a character body that you can modify on your own to get what you want. just add a new script on a character body node and you can select the pre-made script.

it doesn’t have anything in the premade script that I could use to make impulse movement, I’m so lost

a characterbody2D is NOT a “physics” object, it is kinematic. to apply impulse easily you need a RigidBody.

you can simulate more complex physics with character body but you have to do it yourself. there are some very basic physics calculations that you should’ve learned in school or can find in a book, like drag.
the script does apply one of these, and that is gravity.

what you have to understand is that with a characterbody, you set the velocity of it, and it will apply that as a force constantly. the script changes this value every frame based on what you pressed, but you can use += instead of = to add/substract to the value instead of assigning/overriding the value, and that will have a different effect. you can also reduce the final value over time instead of immediately. you can also add a bigger value (this is what jump does).

it depends on what you want to do.

can you show me an example of drag code for the character body2d and if I added drag than adding velocity with += wouldn’t act as impulse, would it? if it does act as impulse than drag code is all I need

Can you describe the kind of movement you want? “Impulse” refers to a one-time impact.

1 Like

I want impulse to work like how apply_central_impulse works with rigid body2d but with character body2d. I have them do a punch and I want them to have some forwards movement with that punch

Okay in that case you can set the velocity to a high value when the character throws a punch. Every frame, decrease the velocity until it reaches 0. This is what deceleration is.

For example

func _physics_process(delta):
    if Input.is_action_just_pressed("punch"):
        velocity.x = 800
    else:
        velocity.x = move_toward(velocity.x, 0, 500 * delta)
    move_and_slide()
1 Like

why is there a 500? wouldn’t it just be 0?

move_toward takes three arguments, the starting value, ending value, and how much you want to move towards the ending value from the starting value. Since you don’t want the player to stop instantly you need to slow it down gradually.

wouldn’t it be 800 than? also I was using this before
func _physics_process(delta):
if velocity.x > 0:
velocity.x += -10
else:
if velocity.x < 0:
velocity.x += 10

do I have to do a move_towards with a different value for every attack?

wouldn’t it be 800 than?

No, 800 is an arbitrary value I gave to the initial impulse when the player punches. It means 800 pixels per second. 500 is an arbitrary value I gave to the deacceleration of the player. It means 500 pixels per second per second, or 500 pixels / seconds ^2.

do I have to do a move_towards with a different value for every attack?

No, you can move_towards 0 for every attack at the same speed.

1 Like

thanks so much dude I got this working well.

1 Like

Hey, the way this works, the character moves at the same speed to one point to other kinda right? how do I make em speed up from the impulse and then slow down (acceleration) The punch works now but is kinda off and I think its because it doesn’t have acceleration, right? how do I add acceleration to this. I needed acceleration in my game at one point anyway

Then you need a way to keep track of whether the player is accelerating or decelerating, like maybe a Boolean variable.

So, have a set time where it swaps to deceleration with the code you showed me? Like a delay even???

Yes, you will have to determine when to stop accelerating and start decelerating.

1 Like

should I use a timer node? I have a lot of attacks I want to add that will most likely use different set times