Sunday, 3 April 2016

Mastering 2D Cameras in Unity: A Tutorial for Game Developers

source: https://www.toptal.com/unity-unity3d/2d-camera-in-unity?utm_campaign=blog_post_2d_camera_in_unity&utm_medium=email&utm_source=blog_subscribers

For a developer, the camera is one of the cornerstones of the game development process. From just showing your game view in a chess app to masterfully directing camera movement in a 3D AAA game to obtain cinematic effects, cameras are basically used in any video game ever made, even before actually being called “cameras”.
In this article I’m going to explain how to design a camera system for 2D games, and I’m also going to explain some points on how to go about implementing it in one of the most popular game engines out there, Unity.

From 2D to 2.5D: An Extensible Camera System

The camera system we are going to design together is modular and extensible. It has a basic core consisting of several components which will ensure the basic functionality, and then various components/effects that can be optionally used, depending on the situation at hand.
The camera system we are building here is targeted at 2D platform games, but can easily extended to other types of 2D games, 2.5D games or even 3D games.
Mastering 2D Camera in Unity: A Tutorial for Game Developers
Mastering 2D Camera in Unity: A Tutorial for Game Developers
I am going to split the camera functionality into two main groups: camera tracking and camera effects.

Tracking

Most of the camera movement we’ll do here will be based on tracking. That is the ability of an object, in this case the camera, to track other objects as they move about in the game scene. The types of tracking that we’ll be implementing are going to solve some common scenarios encountered in 2d platform games, but they can be extended with new types of tracking for other particular scenarios you might have.

Effects

We will be implementing some cool effects like camera shake, camera zoom, camera fade, and color overlay.

Getting Started

Create a new 2D project in Unity and import standard assets, especially the RobotBoy character. Next, create a ground box and add a character instance. You should be able to walk and jump with your character in your current scene. Make sure the camera is set to Orthographic mode (it is set to Perspective by default).

Tracking a Target

The following script will add basic tracking behavior to our main camera. The script must be attached as a component to the main camera in your scene and it exposes a field for assigning a target object to track. Then the script ensures the x and y coordinates of the camera are the same with the object it tracks. All this processing is done during the Update step.
[SerializeField]
protected Transform trackingTarget;

// ...

void Update()
{
    transform.position = new Vector3(trackingTarget.position.x,
         trackingTarget.position.y, transform.position.z);
}
Drag the RobotBoy character from your scene hierarchy over the “Tracking Target” field exposed by our following behavior in order to enable tracking of the main character.

No comments:

Post a Comment