# Gravity acts weird when player is moving

4

### Question

I’m making a simple movement script where the character can move from left to right and can jump and is affected by gravity. The gravity works fine when nothing is being pressed but when the player is pressing left or right the the gravity goes extremely fast and practically snaps them to the floor. How do i fix this?

``````extends CharacterBody2D
@export var speed: int = 140 #How fast you want the character to walk
var is_jumping = false

func _process(delta):
move()
gravity(delta)
print(velocity)
jump()
position += velocity * delta

func move():
if Input.is_anything_pressed() == false:
velocity = Vector2.ZERO
# Handles moving Left and Right
var direction = "Right"#for animations
if  Input.is_action_pressed("Left"):
velocity.x -= speed
direction = "Left"
if  Input.is_action_pressed("Right"):
velocity.x += speed
direction = "Right"
if velocity.x > 140:
velocity.x = 140
if velocity.x < -140:
velocity.x = -140
animations.play("walk" + direction)
func jump():
if Input.is_action_just_pressed("Interact"):
is_jumping = true
velocity.y -= 350
await get_tree().create_timer(0.2).timeout
is_jumping = false

func gravity(delta):
if not is_on_floor():
if is_jumping == false:
velocity.y += 200
position += velocity * delta

func _physics_process(delta):
move_and_slide()
updateAnimation()
``````
1 Like

Don’t change `position` manually, as the`CharacterBody2D` will handle it based on the `velocity` property.

Any code that modifies physics properties, such as `velocity`, or functions that process physics should be run in the `_physics_process()`. Other functions should be run in the `_process()`, because the `_physics_process()` is meant for smoothing physics simulations.

In this case, you may want to run the `move()`, `gravity()`, and `jump()` in the `_physics_process()`, and run the `updateAnimation()` in `_process()`.

1 Like

Create the player by watching a tutorial on youtube like platformer of gdquest, brackeys, etc

1 Like

Good work keep it up.

1 Like

Although @coderjo gives good advice which you should definitely follow, that’s not the actual reason the gravity behaves strangely.

Your current code has constant downward speed applied, rather than acceleration, due to the zeroing of velocity in move(). When input is pressed, this zeroing no longer occurs, and the gravity accumulates so that it is an acceleration rather than constant.

Though you don’t want it to be so fast, acceleration is actually what gravity is supposed to be. It just needs to be adjusted down once you fix the above issue.

Another thing is that you don’t apply gravity for 0.2 seconds just after the jump. This is what allows the jump to function at all in spite of the constant gravity. However, gravity should be applied all of the time, even just after a jump and while on the ground.

So, to fix this, you need to adjust a couple of things:

1. Only set the x component of velocity to 0 in move function when no input is pressed
2. Remove the condition of the gravity not being applied just after the jump (gravity should always be applied, even when on the ground)
3. Adjust the gravity to be lower to account for it now being acceleration rather than a constant speed

Some additional notes / advise not directly related to the original problem:

For your movement, how you currently have it is that it gradually accelerates to gain speed, but then stops instantly when the input is released. Typically movement is either smooth or instant, not.a combination of both.

If you want smooth physics-y movement, you can apply a breaking acceleration, or simply multiply the x velocity by some fraction to decrease it, rather than zeroing it. This would also typically be in addition to the speeding up in opposite direction when input is opposite the current velocity direction.

If you want instant movement, then assign the x velocity, rather than add to / subtract from it.

If you really do want smooth acceleration when gaining speed, but instant stopping, then at least zero it also when pressing in opposite direction to movement. This prevents the player from having to release one direction before pressing in the opposite direction to essentially have the same result.