I wrote a script but it doesn't work, HELP!

Godot Version

extends CharacterBody2D

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

var chase = false
var speed = 100

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
var player = $player/putin
var direction = (player.position - self.position).normalized()
if chase == true:
velocity.x = direction.x * speed

move_and_slide()

func _on_detector_body_entered(body: Node2D) → void:
if body.name == “putin”:
chase = true

I wrote a gravity script for a mob, but it doesn’t work, the mob hangs in the air and doesn’t move, help me find and fix the error

This works in my case, the gravity is applied correctly. Can you show your scene structure and maybe a short video of the game itself how it runs?

PS: You retrieved the gravity into a variable, but then you never use this variable and use get_gravity() instead. Do you need this for something?

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
1 Like

Try adding the gravity to the Y axis of the velocity vector at:

velocity += get_gravity() * delta

1 Like

Why would that make a difference? The gravity returned from get_gravity() is a Vector that usually has only Y component greater than zero, so doing

velocity += get_gravity() * delta

is exactly the same as

velocity.y += get_gravity().y * delta

Unless the gravity is not on the Y axis - but then still the first option is better, because it covers gravity in all directions.

2 Likes

Thank you for your help, I already found the problem myself, it was just a trifle

1 Like