0 votes

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)
Godot version 3.5
in Engine by (23 points)
edited by

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

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.

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) :)

1 Answer

0 votes

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).

by (19,234 points)
edited by

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 = moveandslide(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!

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)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.