Game Development Reference
In-Depth Information
Comparing Update, FixedUpdate, and LateUpdate
There is usually a lot of debate about which update method should be used within a Unity
game. To put it simply, the FixedUpdate method is called on a regular basis throughout
the lifetime of the game and is generally used for physics and time sensitive code. The
Update method, however, is only called after the end of each frame that is drawn to the
screen, as the time taken to draw the screen can vary (due to the number of objects to be
drawn and so on). So, the Update call ends up being fairly irregular.
Note
For more detail on the difference between Update and FixedUpdate see the Unity
Learn tutorial video at http://unity3d.com/learn/tutorials/modules/beginner/scripting/
update-and-fixedupdate .
As the player is being moved by the physics system, it is better to update the camera in the
FixedUpdate method:
void FixedUpdate()
{
// By default the target x and y coordinates of the
camera
// are it's current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;
// If the player has moved beyond the x margin...
if (CheckXMargin())
// the target x coordinate should be a Lerp
between
// the camera's current x position and the
player's
// current x position.
targetX = Mathf.Lerp(transform.position.x,
player.position.x, xSmooth *
Time.fixedDeltaTime );
// If the player has moved beyond the y margin...
if (CheckYMargin())
// the target y coordinate should be a Lerp
Search WWH ::




Custom Search