I want to create an ennemie that follow my player but also hve gravity but some how i can't make my ennemie to be affected by the gravity

Godot Version

Godot 4.3`

Question

I want to create an ennemie that follow my player but also hve gravity but some how i can’t make my ennemie to be affected by the gravity

This is my code:

extends CharacterBody2D

const gravity = 1650
@onready var player = get_node(“/root/World/Player”)

func _physics_process(delta: float) → void:
# Get the direction to the player
var direction = global_position.direction_to(player.global_position)
velocity = direction * 300.0 # Apply movement toward the player

# Apply gravity only if the enemy is not on the ground
if not is_on_floor():  # Checks if the character is not standing on the ground
	velocity.y += gravity * delta
else:
	velocity.y = 0  # Prevent the enemy from floating when on the ground

# Use move_and_slide, which automatically handles velocity and up direction in Godot 4.3
move_and_slide()

This sets both the x and y velocity, maybe you want to only apply direction on the x axis?

velocity.x = direction.x * 300.0