How to make a Spatial node look at another Spatial node

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

I am trying to make a the mask(the visual representation of an enemy, in this case the mask is a 3D model in 3D space) always look at the player, but only on the y-axis(I don’t want it to face the player, only to look). I have some code that doesn’t function correctly. It produces a result but the result does not match what I want.

func _process(delta):
     pPos = player.translation
     mPos = mask.translation
     angle = mPos.angle_to(pPos)
     mask.rotation.y = angle

All of the lines in the code above work. They just constantly produce the same of result of roughly 2.7 and the model is rotated 2.7 radians. The problem isn’t the radians, its the fact that nothing changes. What can I do to fix this code?

What your scene tree looks like? And where the player and the mask nodes are located on the tree?

Dlean Jeans | 2019-06-20 06:41

A picture of the tree

Noddy | 2019-06-21 00:44

The image is not showing. You can instead call print_tree_pretty() and post it here. Remember to format it as a code block with the {} button.

Dlean Jeans | 2019-06-21 06:32

Picture of the scene tree (saw the link in my email):

Scene Tree

Dlean Jeans | 2019-06-21 12:10

:bust_in_silhouette: Reply From: Squabix

Use the look_at() function.

This is look_at() in 2D. The docs for the 3D version is here: Spatial.look_at()

Dlean Jeans | 2019-06-20 06:36

Not to mention the engine version is wrong. I put the engine version in the tags.

Noddy | 2019-06-21 00:57

I don’t think much had changed between 3.0 and 3.1 so it’s okay.

Dlean Jeans | 2019-06-21 06:33

:bust_in_silhouette: Reply From: Dlean Jeans

Try this:

func _process(delta):
     pPos = player.global_transform.origin
     mPos = mask.global_transform.origin
     angle = mPos.angle_to(pPos)
     mask.rotation.y = angle

The docs for Spatial.translation says:

Local translation of this node.

And mask is a child of centipede so its translation is always somewhere around Vector3.ZERO where you put it.