Game Development Reference
In-Depth Information
between
// the camera's current y position and the
player's
// current y position.
targetY = Mathf.Lerp(transform.position.y,
player.position.y, ySmooth *
Time. fixedDeltaTime );
// The target x and y coordinates should not be
larger
// than the maximum or smaller than the minimum.
targetX = Mathf.Clamp(targetX, minXAndY.x,
maxXAndY.x);
targetY = Mathf.Clamp(targetY, minXAndY.y,
maxXAndY.y);
// Set the camera's position to the target position
with
// the same z component.
transform.position =
new Vector3(targetX, targetY,
transform.position.z);
}
Tip
As they say, every game is different and how the camera acts can be different for every
game. In a lot of cases, the camera should be updated in the LateUpdate method after
all drawing, updating, and physics are complete. This, however, can be a double-edged
sword if you rely on math calculations that are affected in the FixedUpdate method,
such as Lerp. It all comes down to tweaking your camera system to work the way you
need it to do.
Once the script is saved, just attach it to the Main Camera element by dragging the script
to it or by adding a script component to the camera and selecting the script.
Finally, we just need to configure the script and the camera to fit our game size as follows:
Search WWH ::




Custom Search