This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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 physicsprocess(delta):
if target:
lookat(target.globaltransform.origin, Vector3.UP)
movetotarget(delta)

func onrangebodyentered(body):
if body.isingroup("Players"):
target = body

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

Godot version 3.5
in Engine by (23 points)

1 Answer

0 votes

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

by (1,055 points)
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.