Set Background Image

In that project, we already had an object can move inside a room, but inside that room is only a blank space. Now we need to add a background to make our game become more beautiful.

We will use this image to set as our game background.

This image is used to make a background, so we don’t have to make it transparent or anything, just let it be. We already mentioned this from one of our previous tutorials.

Now let’s open our project and start to do it.

Adding a background image to Unity is very simple if you’ve already known how to add and use sprite,for anyone who hasn’t known yet, please check this tutorial to learn how to do it.

When you already had a background sprite, let drag and drop it into Hierarchy window or Scene window.

Select it and modify some properties in Inspector window like below:

 

 We set the Scale of it to 4 because we want it to cover all the room that we created from our last tutorial, this Scale also depends on how big the room is. But remember that if you set this Scale to big, then the background image will be blurry, so be careful with that.

We also need to decrease the Order in Layer of it because this is background so it will stand behind all other objects of the game. We don’t want any cake or player will be covered by this image.

Because others objects has the default Order in Layer is 0, so we just need to set this image’s Order in Layer to any integer number which is lower than 0, -1 is an example.

Done! Now we’ve already have a background image for our game.

 LOCK CAMERA TO PLAYER WHEN MOVING

One thing you can see in our game now is that when the player (pacman) is moving outside the camera’sview, we can’t see it anymore. So we need to follow it to anywhere inside the room.

To do this, we need a script to control camera’s position when the player is moving.

Before doing anything, we need to add tag to our player, to make sure that Main Camera can find it easily.

Select pacman object, select Tag -> Choose the Player tag.

 

This tag is one of default tags in Unity, we can use it for our player.

Create a newC#Script in Scripts folder and set name for it something like FollowPlayer.

Select Main Camera, drag the FollowPlayer script into the blank space of Inspector windowto add this script as Main Camera’s component.

Or you can click on Add Component ->Scripts -> Select FollowPlayerscript

 

Open it in Script Editor then add some lines of code inside class like this:

//Declare variable to store the Transform component of player

Transform playerTransform;

void Start (){

//Find the Transform of player and set it to variable

playerTransform=GameObject.FindGameObjectWithTag("Player").transform;

}

 

// Update is called once per frame

void Update (){

//Change the position of camera to the position of player but keep its z position

transform.position=new Vector3(playerTransform.position.x,playerTransform.position.y,transform.position.z);

}

 

First, we declare a variable type Transform method to store Transform component of our player.

When the game starts, Start() method will be call and the Transform component of player with its tag is “Player” will be set for that variable.

Inside the Update() method:

transform.position this Transform without any object before is the Transform of the object that this script is attached to. In this case, it’s theTransform component ofMain Camera.

We set the position of Camera to the position of player to keep the player always inside the Camera’s view. The position of Camera will take only the x and y coordinatesfrom player position, the z coordinate is not used in 2D game so we can keep the old z coordinate of it.

That’s all we need to make a Main Camera to follow player. Now you can come back to Unity and enter play mode to test it.

If you see the pacman face is always at center of game screen, it means we’re success to make camera to follow player.

If you still have any trouble or just want to ask us any question, leave us a comment, we will response to you as soon as possible.

Thanks for reading and see you again in our next tutorial.