3D How to add gravity to enemies?

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

im struggling to add gravity to the enemies in my game. if i jump on a ledge or something that high up they will float towards me without gravity effecting them.
this is what ive tried so far. Sorry for the bad paste of code, no matter what i do here it wont look right, in the editor the indentation is correct.

var target
export var speed = 350
var health = 100
var gravity = -350
var velocity = Vector3()

func _physics_process(delta):
if target:
look_at(target.global_transform.origin, Vector3.UP)

func _on_range_body_entered(body):
if body.is_in_group(“Players”):
target = body

func move_to_target(delta):
velocity.y += gravity * delta
var dir = (target.transform.origin - transform.origin).normalized()
velocity = move_and_slide(dir * speed * delta, Vector3.UP)

:bust_in_silhouette: Reply From: SteveSmith

You’re not actually using ‘velocity’, only setting it. I would adjust dir.y by a gravity value just before you call move_and_slide.