Following on from the (Blog Post name for design) blog post, I went back into Unity to create a method allowing the player to zoom in and out of the screen so they are able to view the whole map and make better decisions whilst playing. Adding this in now will also come in handy when starting each level of the game, as the camera will pan out to show the player the whole scene to give them an idea of what they need to do in order to progress.
To start working through this I looked at the camera in the hierarchy and changed the settings to see how it would affect the game view. Changing the size of the camera using the scroll wheel in the inspector zoomed it in and out of the target position (that being the player). So, all I needed to do for the zoom was to work out how to change the size of the camera using an input. All notes for working and constructing this script can be found on page 1-8 of the “Coding notes book”.
Before writing the script I needed to add another Input in the project settings so the PS4 controller can be used to zoom in and out of the scene. I decided to use the right joystick for this. So, the input “RightJoystickV” was a reference to the right joystick moving up and down and these movements would zoom the camera in and out respectively. In the script, I referenced the Orthographic size (camera size) in the update so this could be altered using the inputs of the controller. The code for this can be found below:
Now this was added, I could use the Inputs to change its size which I also added into the update method. I asked the engine if the right joystick was being pressed down, and if it was, the camera size would be increased at a rate of 0.2 each time the input was being met (zooming out). Below that was the second method to ask if the right joystick was being pressed up, and if so, the size was decreased at the same rate (zooming in). However, when testing, pressing the joystick down increased the camera size but continued to do so despite letting go of the joystick and even when pressing the joystick up, which should have decreased its size. The video for this testing can be found below and now meant I needed a new method for this.
After researching into what Input Axis can be used by the controller, the L2 and R2 buttons are a reference to the 5th Axis so can be used for the camera zoom. The manual states that unpressed = -1f in the Axis and up to 1f means it is pressed. So, for this I created the L2 button on the input menu and had to change scripting. The first if statement changed to ask if the input was equal to 1 (pressed), and if so, the camera sized increased. Then, if the input was equal to -1 (not pressed) the camera size would decrease and zoom back into the scene. This worked for zooming in and out of the scene but when zooming back in it carried on past the original camera size and away from the scene. The video and code for this can be found below:
This had my brain hurting for a little while, but finally realised it was an easy fix. In the zoom in method, I created a second if statement asking if the camera size was below 35 (the original size), and if so, the camera size was set to 35. This meant that whenever camera size went any value below 35 it was immediately set back to 35. The video for the final test can be found below:
As we have not yet put in the artwork for the scenes, the camera zoom will have to be changed at a later stage of production so none of the scene below the floor is shown.