Game Development Reference
In-Depth Information
var q_yRotation : Quaternion = Quaternion.
LookRotation(Vector3(v3_offsetToCenter.x, v3_offsetToCenter.y +
height, v3_offsetToCenter.z));
//Apply the rotation to the camera
var v3_relativeOffset = Vector3.forward * distance + Vector3.
down * height;
cameraTransform.rotation = q_yRotation * Quaternion.
LookRotation(v3_relativeOffset);
}
So, we are done with this chapter. We can go to Unity and click Play to see our result. We will
see that now the camera is following our character.
Objective Complete - Mini Debriefing
We just created a third-person camera to follow our character. This script also allows us to
set the distance from our character and the height of our camera posiion by using some
code from the third-person camera built-in script and adaping it to our character.
Classified Intel
Why do we need the LateUpdate() funcion instead of the Update() funcion for this
script? Well, we used it to guarantee that the player posiion is already updated when we
are doing the camera calculaions. If we are doing the calculaion in the Update() funcion,
the camera posiion might be calculated before the player posiion is updated. This will result
in jiter.
We can also explain it this way: We wait for the input from the user and then get the
direcion where the character will go in the Update() funcion. Then, we use the posiion
of the character as the target posiion that our camera will follow, and calculate the camera
posiion in the LateUpdate() funcion. This way, we will be able to track each movement
of our character and the camera will follow the direcion smoothly without any jiter, as we
can see in the following diagram:
 
Search WWH ::




Custom Search