#3 LookAt Method

Introduction

In this arti­cle we're going to ana­lyze how to apply the LookAt func­tion in Uni­ty, to make a GameOb­ject look in the direc­tion of anoth­er GameObject.

Procedure

We are going to work with the Look At sta¬≠tion, which con¬≠sists of a con¬≠tain¬≠ment cap¬≠sule in which Lucy's mod¬≠el is float¬≠ing. The goal is to get Lucy to fol¬≠low us with her gaze all over the stage.

Go to the project's main page

Fig. 1: Lucy is inside this capsule. 

In the hier­ar­chy we have a GameOb­ject called "#3 LookAtObject".

Fig. 2: Hier­ar­chy of the scene. The LookAt sta­tion is GameOb­ject #3.

This GameOb­ject is assigned the "LookA­tO­b­ject" Script, which will see to it that Lucy fol­lows her gaze on the player.

Fig. 3: Script to com­plete in this challenge.

Fig­ure 4 shows that there are 3 fields to com­plete, the first is for the observ­er object, the sec­ond is for the obser­va­tion objec­tive and the third is for Lucy to look at a fixed height of the stage, so she does not leave the bor­der of her cap­sule containment.

Fig. 4: Script assigned to the GameOb¬≠ject "#3 LookA¬≠tO¬≠b¬≠ject" shown in fig¬≠ure 2.

We're going to assign Lucy direct¬≠ly from the hier¬≠ar¬≠chy and we're going to find the char¬≠ac¬≠ter through her tag.

If you want to learn dif­fer­ent ways to find the ref­er­ences of the GameOb­jects of the hier­ar­chy to use in the Scripts you can read the pre­vi­ous arti­cle or watch this video with sub­ti­tles.

If we open the script we find some things done. In the FixedUp­date method, which is exe­cut­ed once every so often, we make the call to the "lookA­tO­b­ject" method, which will ful­fill the function.

Fig. 5: Script LookA­tO­b­ject sin completar.

In the start method seen in fig­ure 5, we have an instruc­tion that was com­ment­ed at the time of mak­ing the cap­ture. You have to remove the com­ment bars to find the player's ref­er­ence and get Lucy to look at it.

Fig. 6: Fields defined in the Script.

Fig¬≠ure 6 shows the fields defined in the Script. We are going to use the Vector3 called "object¬≠ToLook¬≠Po¬≠si¬≠tion" for the coor¬≠di¬≠nates that Lucy must look at.

Fig. 7: With these two instruc­tions we get Lucy to look at the ori­gin of the character.

With­in the "lookA­tO­b­ject" method we write these instructions:

object­ToLook­Po­si­tion = objectToLook.transform.position;

objectThatLooks.transform.LookAt(objectToLookPosition);

With these two instruc­tions, Lucy is always ori­ent­ed towards the player's origin.

Fig. 8: Lucy sigue al per­son­aje con la mirada.

Fig. 9: When approach­ing the cap­sule, Lucy leans so far that she goes beyond the bound­aries of the capsule.

The chal¬≠lenge is par¬≠tial¬≠ly solved, we still have to solve the prob¬≠lem illus¬≠trat¬≠ed in fig¬≠ure 9.

Fig. 10: Three GameOb¬≠jects show¬≠ing their ori¬≠gins. The ori¬≠gin is rep¬≠re¬≠sent¬≠ed by a giz¬≠mo show¬≠ing the local axes.

First let's under¬≠stand what's going on.

All GameOb­jects have a coor­di­nate in the space where their ori­gin is located.

The LookAt method will mod­i­fy the ori­en­ta­tion of the object to which it is applied (in this case Lucy). It will do this in such a way that the local for­ward axis points to the ori­gin of the object to look at. As illus­trat­ed in the fol­low­ing figure.

Fig. 11: Lucy with the At look method applied, char¬≠ac¬≠ter away.

If the char­ac­ter is far from Lucy his incli­na­tion is small, but when he gets clos­er the fol­low­ing happens:

Fig. 11: Lucy with the applied lookAt method, char¬≠ac¬≠ter in close proximity. 

How can we cor¬≠rect this?

We need to get Lucy to look at a coor¬≠di¬≠nate that's at the same height as her ori¬≠gin. That way her tilt angle will be small. 

So we are going to over­write the com­po­nent and of Vector3 by the val­ue of the vari­able "yPos" that we will adjust from the inspector.

Ide­al­ly we could make the com­po­nent and the Vector3 worth exact­ly the height of Lucy's ori­gin, this is some­thing I real­ized lat­er in the post pro­duc­tion of the video. How­ev­er it is good as an exam­ple that there is no sin­gle way to solve the problems.

Fig. 12: An instruc¬≠tion is added between means that over¬≠writes the val¬≠ue of the com¬≠po¬≠nent and the Vector3. 

You can try writ¬≠ing the vari¬≠able yPos at run¬≠time, but when you stop the sim¬≠u¬≠la¬≠tion all changed val¬≠ues will return to their ini¬≠tial value.

The val­ue 0.75 works well for this case. A num­ber less than this caus­es Lucy to lean down and come out the front of the capsule.

Fig. 13: The val¬≠ue ‚ÄĎ0.3 caus¬≠es Lucy to exit from the front of the capsule. 

Fig. 14: With yPos equal to 0.75 Lucy looks cor¬≠rect¬≠ly at the character. 

Fig. 15: With yPos equal to 0.75 Lucy looks cor¬≠rect¬≠ly at the character. 

In the fol­low­ing arti­cle we will study how to cre­ate and destroy objects at run­time. Sub­scribe to the YouTube chan­nel to stay on top of new videos and articles.

CHECK OUT THIS CRAZY EXPERIMENT

YouTube
Scroll to Top
Secured By miniOrange