In this article we’re going to analyze how to apply the LookAt method in Unity, to make one GameObject look in the direction of another.
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.
In the hierarchy we have a GameObject called “#3 LookAtObject”.
This GameObject is assigned the “LookAtObject” Script, which will see to it that Lucy follows her gaze on the player.
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.
We’re going to assign Lucy directly from the hierarchy and we’re going to find the character through her tag.
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.
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.
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.
Within the “lookAtObject” method we write these instructions:
objectToLookPosition = objectToLook.transform.position;
With these two instructions, Lucy is always oriented towards the player’s origin.
The challenge is partially solved, we still have to solve the problem illustrated in figure 9.
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.
If the character is far from Lucy his inclination is small, but when he gets closer the following happens:
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.
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.
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.