Kinematic Body facing another Kinematic body 3d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Wodsobe Dobe

How would I make on Kinematic body face another kinematic.body in a 3d space? and How would I apply gravity to another kinematic body with AI? Here’s some code I have from another user:
func _physics_process(delta):
var to_player = translation.direction_to(player.translation)
move_and_slide(to_player * speed)

but gravity doesn’t work and neither does the rotation. He just faces one way. Anybody know what top do?

Try the look_at and look_at_from_position functions. They do pretty much what you’re asking for. E.g. look_at(player.global_transform.origin, Vector3(0,1,0)). The second parameter is the up vector.

Michael Antkiewicz | 2020-01-17 00:28

I’ll test it out and see if it works! It looks like it should just by reading it! Thanks!

Wodsobe Dobe | 2020-01-17 01:07

He’s facing towards me! Only one problem, he can look down and up too, resulting in his whole body tilting when I just want him to just stay locked as straight up and down on the x axis. how to I fix that? Also, thanks for the code. It helped a lot.

Wodsobe Dobe | 2020-01-17 03:17

Use only x and z in the look at and replace the y with your “own” y then it’ll look “level”.

look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0))

But how do you imagine gravity with AI in your game? Does the gravity have a life on its own?

Basic gravity (without using RigidBodies and physics) would probably work by getting the current y-velocity by comparing the bodies previous (physics frame) origin with the current origin. Then add a constant value to that y-velocity and add it as y-component to the next move_and_slide

wombatstampede | 2020-01-17 14:02

The rotation problem fix freaking worked @wobatstampede ! I don’t know how to add the gravity thing to the move and slide, where do I do that in the values? Also thank you for helping me with the rotation problem!

Wodsobe Dobe | 2020-01-18 02:10

Gravity usually let’s things move down. So in direction -y.
So add some downward y-value to your move_and_slide target. This is i.e. by adding some Vector3(0,-down_speed,0).

Like I said, for a start you might want to compare the transform.origin.y with the origin.y of the previous physics frame (save it in some variable) and then increase it with some fixed value.

wombatstampede | 2020-01-18 15:37

Thanks! Everything works now!

Wodsobe Dobe | 2020-04-11 22:06