hi i have problem with my jump and gravity script my character shuld fall down but it stanting in one place

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mttrafik

here is script

extends KinematicBody2D

var gravity = 100
var jumpheight = 100
var velocity = Vector2.ZERO

func _physics_process(delta):
	if Input.is_action_pressed("jump"):
		velocity.y -= jumpheight
		velocity.y += gravity * delta
		velocity = move_and_slide(velocity, Vector2.UP)

A few things here:

  • Don’t post the same message multiple times. That only serves to further fracture limited resources.
  • Format any code you paste into the forum. Otherwise, it’s hard to read and confuses the markup handler. To do that, paste your code, select it, press the {} button to format it, look at the Preview panel to ensure proper formatting.
  • Like your previous post on this topic, you haven’t described the problem. What’s wrong?
  • Post your actual Godot version. Latest is meaningless over time…

jgodfrey | 2023-02-21 19:44

(again), edited to fix code formatting.

jgodfrey | 2023-02-21 19:45

:bust_in_silhouette: Reply From: exuin

It’s because you only apply gravity and move when you jump, move the last two lines out of the if statement

In addition to what exuin said, but you probably also want to check that the character is on the ground before allowing them to jump (unless you want the character to keep adding to the jump every frame they hold down the jump button)

I don’t know if your game is going to be something like a platformer, but you can see a nice tutorial about implementing platformer movement here

AndyCampbell | 2023-02-26 22:26