+1 vote

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?

in Engine by (144 points)

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

A picture of the tree

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.

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

Scene Tree

2 Answers

0 votes
by (32 points)

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

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

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

+1 vote

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.

by (4,155 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 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 webmaster@godotengine.org with your username.