3D How to add gravity to ai 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)
	move_to_target(delta)

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)

To fix the forum code formatting…

  • Edit your post
  • Select the code block
  • Press the { } button in the forum editor’s toolbar.
  • Look in the Preview panel to ensure proper formatting
  • Save your changes

jgodfrey | 2022-09-28 19:24

well it looks good on my end now. It takes a while to paste code like this, i have to use the button for every line rather than just paste it all inside sadly.

King C rocodile | 2022-09-28 19:49

Hmmm… That shouldn’t be the case. Worst case (if the formatting in the OP is badly broken) - in the edited post you should be able to eliminate the original code, paste it again (from Godot), select it, and press the { }.

The key to code format here is leading spaces. Anything with at least 4 leading spaces is rendered as code. That’s all the { } does…

Anyway, the post looks better (except for that first code line) :slight_smile:

jgodfrey | 2022-09-28 20:19

:bust_in_silhouette: Reply From: jgodfrey

While you’re attempting to account for gravity here:

velocity.y += gravity * delta

That adjusted value is never used for anything. Normally, you’d calculate velocity and then pass it into move_and_slide() as the first argument. Additionally, you’d store the return value from that call back in velocity.

A typical call would look like this:

velocity = move_and_slide(velocity, Vector2.UP)

Also note, as documented, move_and_slide() automatically calculates frame based movement, so you should not use delta in the velocity argument (as your original code does).

I actually did try that once but i don’t know what to do with the (dir * speed* delta).
This is what i tried, velocity = move_and_slide(dir * speed * delta, velocity, Vector3.UP)
But that gave me an error, i think too many arguments. May i have possibly been just doing it in the wrong order or put velocity in the wrong place then?
Also thanks for answering, didn’t know that about move and slide!

King C rocodile | 2022-09-28 20:50

Based on your existing code, I’d suggest you try something like this:

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

jgodfrey | 2022-09-28 20:58

Il have to do that tomorrow but it looks to be exactly what i need. Thanks a lot!

King C rocodile | 2022-09-28 21:04