How to add acceleration to character body2d

Godot Version

4

Question

How do I add acceleration to the character body2d, I don't know how to code acceleration

Acceleration is a change in velocity over time. You will need to multiply the change by delta to take time into account. So something like this:

func _physics_process(delta):
    velocity.x = move_toward(velocity.x, target_velocity, acceleration * delta)
3 Likes

what do you mean by acceleration here and is target_vleocity just example 0, 500?

Can you please get back to me with this?

I think I already explained what acceleration is in a different question you asked.

1 Like

that was drag, you helped me with drag

It’s a similar concept. Read the docs on move_toward

You take your current velocity, and move it towards a target velocity, by an acceleration amount per second (* delta).

target_velocity is how fast (and in what direction) you want the player to move, usually this is calculated with inputs times a max speed.

var target_velocity = Input.get_axis("Left", "Right") * SPEED

acceleration is how fast you want the player to reach that max speed, in pixels per second so it may require a very large number, I recommend around eight times the max speed.

1 Like

can you fill this is with example numbers

func _physics_process(delta):
velocity.x = move_toward(velocity.x, target_velocity, acceleration * delta)

cause right now my brains like

func _physics_process(delta):
velocity.x = move_toward(velocity.x, 0, 500 , 1 * delta)

but that doesn’t work

I get all the target_velocity now but I’m not sure about the acceleration, how do I make acceleration because I know what acceleration itself is

It’s a number, like speed only higher.

const acceleration: float = 4000
1 Like

I know its a number, but I can’t figure out how to put it into code

I kinda got it into code but its so damm weird is doesn’t feel right,

Could you paste your code? In what ways does it feel weird? Make sure to tweak the numbers

1 Like

var acceleration = 0

func _physics_process(delta):

if acceleration > 0:
	acceleration += -150 * delta
	velocity.x += 1000 * delta
else:
	if acceleration < 0:
		acceleration += 70 * delta
		velocity.x += -400 * delta

whenever I do a punch:
acceleration += -10

Yeah that does seem wildly different from the other code posted. Do you use move_toward anywhere in your code? Do you use Input or is this not a player controlled object?

Based only on what you pasted acceleration would have no affect on your speed or how quickly you gain speed.


You can paste code between three ticks ``` for proper formatting

1 Like

When I posted that other code, I was in a rush so here is my acceleration code, it stinks so I want to make acceleration code that doesn’t stink

var acceleration = 0

func _physics_process(delta):

   if acceleration > 0:
	   acceleration += -150 * delta
	   velocity.x += 1000 * delta
   else:
   	   if acceleration < 0:
		   acceleration += 70 * delta
		   velocity.x += -1000* delta

   velocity.x = move_toward(velocity.x, 0, 300 * delta)

whenever I do a punch:
   acceleration += 10
   velocity.x += 10

this kinda works by the way ( I have move_and_slide too)
I don’t use input, it is a player controlled object but Is basically the same as input

Your pasted code does not use acceleration to calculate speed, I have no idea what unit it’s supposed to be. Your move_toward should use acceleration as a constant like in the examples provided


You will need to recieve input somewhere for this to be a player controlled object, make a target_velocity and define a maximum speed however you must. It seems like you are trying to use acceleration for a Input substitute, but it also interacts with punching.

1 Like

input as in a key being pressed to do the punch? I pretty much have that so don’t worry unless unput is something else

are you saying if I copy and pasted this code it just works acceleration and drag in together. const acceleration: float = 4000 func _physics_process(delta): velocity.x = move_toward(velocity.x, target_velocity, acceleration * delta)
Is target_velocity just how much I want the player to accelerate to? should the punch set target_velocity to something? should target_velocity be a var, is target_velocity just a number that never changes? by adding to the velocity.x does that just work with acceleration and drag now instead of only drag. how course I have copyed this code poorly so please take note that I’m referring to code you had in you last post by tomato

Input as in pressing a button yes, a function like Input.get_axis will give you a positive number for moving right, and a negative number for moving left.

  1. Yes, it will be the player’s desired speed
  2. I assume not, I still do not know how you want punching to interact with speed
  3. target_velocity should be a var, it will change. for example changing between a negative number to go left, a positive number to go right, and zero to stay still
1 Like

ok so in here move_toward(velocity.x, target_velocity, acceleration * delta)
went I make target_velocity = to a value, it just keeps moving and doesn’t slow down to start to stop. what makes the target_velocity go to 0 after it got the move_towards makes it go to the target_velocity? should I make it so when the velocity.x is equal to what I set target_velocity to than set target_velocity to 0.