How do you make an object look at another object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dava
:warning: Old Version Published before Godot 3 was released.

I have already done all the animation for that, it’s just the scripting part, I am relatively new to gdscript, I tried

if player.getpos(x) < object.getpos(x):
    #player looks at the object whether going forward or backward
elif player.getpos(x) > object.getpos(x):
    #player looks backwards at the object whether going forward or backward
if player.getpos(y) > object.getpos(y):
     #player looks down at the object whether going forward or backward

None worked. I want the player to start looking at the object as soon as the object enters the camera. This is intended for finding secrets in a level

Maybe you should make add the player’s direction to the if?

Bojidar Marinov | 2016-07-20 11:42

:bust_in_silhouette: Reply From: splite

Well, you have to use global positions (get_global_pos()), not local.

As for looking at, there is sample “look at” project in your godot instalation :slight_smile: Just run it from Project list, its very simple piece of code, arrows are pointing at cursor. Maybe you will have to add or substract PI/2 or PI/4, becouse arrows in that sample project are “heading down”, “y is front” (or idk how to say it…) and i am pretty sure head of your character isnt :slight_smile: You will see, feel free to ask another question :wink:

And, last thing, you have to add VisibilityNotifier2D on your secret location (but beware known bug #1269) connect to enter_screen and exit_screen signals (events) to start your look at procedure. You can add_to_group them in “secrets” group, find them get_tree().get_nodes_in_group("secrets") and connect with simple foreach statement (see tutorial)

edit: get_global_position() is of course get_global_pos()