This part is just about creating a camera and a level to use in our platform game. We will be creating a camera that will show all the objects in the scene and follow our character movement.
Before we start creating this project, we will create the project in Unity by following these steps:
Chapter1
package folder that you downloaded into the project assets folder, by copying it into the project's Assets folder or drag-and-dropping it into the Unity window, as we can see in the following screenshot:Plane
and background.png
in your Project
folder, as shown in the following screenshot:Plane
object in the Project view to bring up its Inspector view. Next, we go to the FBXImporter | Meshes component, and set the Scale Factor to 1, as shown in the following screenshot, and click on the Apply button:We are now ready to start, so let's get on with it!
Plane
prefab object in the FBX
folder—go to the Project view, click on the Plane prefab object, and drag it into the Hierarchy view.There is also the Unity built-in Plane
object that you can use, but you don't really want to use it, because the Unity built-in Plane
object will have way too many triangles for our 2D objects. As we can see from the following screenshot, our prefab Plane
only has two triangles, but the Unity built-in Plane
object will have around 200 triangles.
This will bring up the pop-up window, as shown in the following screenshot. Click on the Continue button to break the prefab:
background.png
file from the Chapter1/Level
folder in the Project view and drop it in the texture thumbnail, and then set the following:Now, we are adding our material to the background object, click on Background
object in the Hierarchy view to open the Inspector view, and in Mesh Renderer | Materials, set the parameters as follows:
Tag
and Layer
for our Background
object; go to Edit | Project Settings | Tags and click on the arrow next to the Tags option to open it, as shown in the following screenshot:For Element 0 type Background, for Element 1 type Floor, for Element 2 type Wall, and then for User Layer 8 type Background, for User Layer 9 type Level; we select our Background
object, and then go back to the Background
object's Inspector view setup as follows:
So, now we have our Floor cube and Wall cube.
Floor
and Wall
objects by dragging the material Floor
and Wall
objects in the Hierarchy view. Then we drag-and-drop Floor
and Wall
inside our Level
object, as shown in the following screenshot:floor
object in the hierarchy, and press Command + D for Mac users or Ctrl + D for Windows users to copy it six times, and click on the wall
object in the hierarchy and copy it twice. So now we have seven floor
objects and three wall
objects.floor
and wall
objects. Let's set them up as follows:Basically, what we have done here is create a Background
object behind the Level
object, and set the Main Camera
in front of the Level
object. Our Main Camera
will also follow our character while he is moving. This way we can make sure that the player will always see our character and background image. We can set our scene and level, as shown in the following diagram:
In our Main Camera
, we set the Projection to Perspective because we want to show the thickness of our level and the depth of the object, which will give a nice view for the player.
We can set the Camera Projection in our scene to be either Orthographic or Perspective. The difference between both projections is that with the Orthographic Projection, the object won't scale by the distance of the camera. So in our scene, we will see only one side of the object that faces the camera. On the other hand, in Perspective Projection we will see the depth of the object that will scale down by the distance of the camera, which is very similar to real life.
In our scene, we won't see any significant difference on our background object because our background object is a plane and doesn't have any thickness on it, but if we are trying to adjust the Projection of our camera, we will see the difference between the two projections. We can do this by going to the Hierarchy view, clicking on Main Camera, changing Projection to Orthographic, and Size to 8.5, and then changing Projection back to Perspective. The difference is shown in the following screenshot:
3.15.221.133