Gravity isn't working

Godot Version

v4.3

Question

I have no idea why I can’t make my character to fall

extends CharacterBody2D

var speed = 300
var jumpForce = 2000
var gravity = 500

func _physics_process(delta: float) -> void:
	velocity.y += gravity * delta
	look_at(get_global_mouse_position())
	rotation -= deg_to_rad(-90)
	if Input.is_action_just_pressed("Jump"):
		jump_to_mouse()
		move_and_slide()

func jump_to_mouse():
	var mouse_position = get_global_mouse_position()
	var direction = (mouse_position - global_position).normalized()
	velocity = direction * jumpForce

I am using a character body 2d with a collision shape 2d and a sprite thats it

You’re not moving unless you are pressing “Jump”, you need to call move_and_slide every frame

2 Likes

Just move move_and_slide() one tab space? back.
It should look like this:

extends CharacterBody2D

var speed = 300
var jumpForce = 2000
var gravity = 500

func _physics_process(delta: float) -> void:
	velocity.y += gravity * delta
	look_at(get_global_mouse_position())
	rotation -= deg_to_rad(-90)
	if Input.is_action_just_pressed("Jump"):
		jump_to_mouse()
	move_and_slide()

func jump_to_mouse():
	var mouse_position = get_global_mouse_position()
	var direction = (mouse_position - global_position).normalized()
	velocity = direction * jumpForce
1 Like

thanks