Making the camera move
The first thing you hopefully noticed when we tested the scene in the previous chapter was that we were just looking in to the distance at nothing. That won't do for a game, traditionally the camera follows the player around the level and responds to their movements. The character in our game at the moment is represented by a blue cube, in our BuildLevel
method of our Sokoban
script, we assign a name to the player when we instantiate it; to refresh your memory the code in question is as follows:
thePlayer = Instantiate(player,new Vector3 (i,0, j),Quaternion.identity) as Transform; thePlayer.name = "Player";
Giving the instantiated GameObject
a name of Player
means we will be able to address the Player
game object from other scripts when we need to reference it.
Let's create a new script for making the Main Camera focus on the player and rotate according to the player's orientation. Right-click on the Scripts
folder that you created earlier and then click on Create and...