Introduction

In this article we’re going to analyze how to apply the LookAt function in Unity, to make a GameObject look in the direction of another GameObject.

Procedure

We are going to work with the Look At station, which consists of a containment capsule in which Lucy’s model is floating. The goal is to get Lucy to follow us with her gaze all over the stage.

Go to the project’s main page

Fig. 1: Lucy is inside this capsule.

In the hierarchy we have a GameObject called “#3 LookAtObject”.

Fig. 2: Hierarchy of the scene. The LookAt station is GameObject #3.

This GameObject is assigned the “LookAtObject” Script, which will see to it that Lucy follows her gaze on the player.

Fig. 3: Script to complete in this challenge.

Figure 4 shows that there are 3 fields to complete, the first is for the observer object, the second is for the observation objective and the third is for Lucy to look at a fixed height of the stage, so she does not leave the border of her capsule containment.

Fig. 4: Script assigned to the GameObject “#3 LookAtObject” shown in figure 2.

We’re going to assign Lucy directly from the hierarchy and we’re going to find the character through her tag.

If you want to learn different ways to find the references of the GameObjects of the hierarchy to use in the Scripts you can read the previous article or watch this video with subtitles.

If we open the script we find some things done. In the FixedUpdate method, which is executed once every so often, we make the call to the “lookAtObject” method, which will fulfill the function.

Fig. 5: Script LookAtObject sin completar.

In the start method seen in figure 5, we have an instruction that was commented at the time of making the capture. You have to remove the comment bars to find the player’s reference and get Lucy to look at it.

Fig. 6: Fields defined in the Script.

Figure 6 shows the fields defined in the Script. We are going to use the Vector3 called “objectToLookPosition” for the coordinates that Lucy must look at.

Fig. 7: With these two instructions we get Lucy to look at the origin of the character.

Within the “lookAtObject” method we write these instructions:

objectToLookPosition = objectToLook.transform.position;

objectThatLooks.transform.LookAt(objectToLookPosition);

With these two instructions, Lucy is always oriented towards the player’s origin.

Fig. 8: Lucy sigue al personaje con la mirada.

Fig. 9: When approaching the capsule, Lucy leans so far that she goes beyond the boundaries of the capsule.

The challenge is partially solved, we still have to solve the problem illustrated in figure 9.

Fig. 10: Three GameObjects showing their origins. The origin is represented by a gizmo showing the local axes.

First let’s understand what’s going on.

All GameObjects have a coordinate in the space where their origin is located.

The LookAt method will modify the orientation of the object to which it is applied (in this case Lucy). It will do this in such a way that the local forward axis points to the origin of the object to look at. As illustrated in the following figure.

Fig. 11: Lucy with the At look method applied, character away.

If the character is far from Lucy his inclination is small, but when he gets closer the following happens:

Fig. 11: Lucy with the applied lookAt method, character in close proximity.

How can we correct this?

We need to get Lucy to look at a coordinate that’s at the same height as her origin. That way her tilt angle will be small.

So we are going to overwrite the component and of Vector3 by the value of the variable “yPos” that we will adjust from the inspector.

Ideally we could make the component and the Vector3 worth exactly the height of Lucy’s origin, this is something I realized later in the post production of the video. However it is good as an example that there is no single way to solve the problems.

Fig. 12: An instruction is added between means that overwrites the value of the component and the Vector3.

You can try writing the variable yPos at runtime, but when you stop the simulation all changed values will return to their initial value.

The value 0.75 works well for this case. A number less than this causes Lucy to lean down and come out the front of the capsule.

Fig. 13: The value -0.3 causes Lucy to exit from the front of the capsule.

Fig. 14: With yPos equal to 0.75 Lucy looks correctly at the character.

Fig. 15: With yPos equal to 0.75 Lucy looks correctly at the character.

In the following article we will study how to create and destroy objects at runtime. Subscribe to the YouTube channel to stay on top of new videos and articles.

Scroll to Top
Secured By miniOrange