Now, with a traditional first-person shooter, we shoot a bullet from a gun to damage enemies. However, we will create a camera that will take pictures to damage enemies.
Now before we can shoot anything, we need to make our camera, and to do this, we are going to create another camera object that is zoomed in and then give it a border to look more realistic. To show how easy it is to do, let's get started! Perform the following steps:
0
, 0
, 0
. If it isn't, set it at that position. Finally, name the object Photo Camera
. Take a look at the following screenshot:Camera
in the search bar before selecting it.30
. Now, switch the Scene tab over the Game tab so you can see what we will be doing. This will aid in understanding what each of these properties do.1
(higher numbers put things in front of the other cameras)..125
and the W and H properties to .75
.Chapter 9/Art Assets
folder included in the example code, grab the phone.png
file and move it into the Materials
folder inside your project browser..png
files will be imported with a Texture Type value of Texture
. We want to change it to Sprite (2D and UI)
, so click on Apply.If you play the game at this point, the game should look something like the following screenshot:
Looks pretty good! But right now, the camera is always up there, and I only want to see it if the player right-clicks to zoom in.
PhoneBehaviour
and open it inside of your IDE.PhoneBehaviour
class as follows:using UnityEngine; using System.Collections.Generic; // List public class PhoneBehaviour : MonoBehaviour { public List<GameObject> phoneObjects; private bool cameraActive = false; void Start() { SetCameraActive(false); } // Update is called once per frame void Update () { // Are we holding down the right mouse button if (Input.GetMouseButton(1) && !cameraActive) { SetCameraActive(true); } else if(cameraActive && !Input.GetMouseButton(1)) { SetCameraActive(false); } } void SetCameraActive(bool active) { cameraActive = active; foreach (var obj in phoneObjects) { obj.SetActive(active); } } }
This script basically makes it so when the player holds down the right mouse button, we will turn on all of the objects that we placed in phoneObjects
and then when we release, we turn them off. We only want to do this on frames when it changes from one state to another (turning it on every frame would be inefficient), so we added in a Boolean to keep track where we were in the last frame.
Go in and add the PhoneBehaviour component to the FirstPersonCharacter object. From there change the Size of Phone Objects to 2
and assign Photo Camera and our Canvas' Panel to the property.
Save our project, level, and play the game!
Now, the camera will only come up whenever we hold down the right mouse button! Awesome! It's just like a sniper rifle in most FPS games.
Next, we want to add the ability to shoot our weapon and flash it on the screen whenever the camera is shot. To simulate this behavior, we will first create an image to be placed over our entire screen. We perform the following steps:
Phone Border
.PhoneBehaviour
file, and add in the following using
statement:using UnityEngine.UI; // Image
public Image cameraFlash;
Fade
. using
statement:using System.Collections; // IEnumerator
IEnumerator Fade(float start, float end, float length, Image currentObject) { if (currentObject.color.a == start) { Color curColor; for (float i = 0.0f; i < 1.0f; i += Time.deltaTime * (1 / length)) { /* Cannot modify the color property directly, so we need to create a copy */ curColor = currentObject.color; /* Do a linear interpolation of the value of the transparency from the start value to the end value in equal increments */ curColor.a = Mathf.Lerp(start, end, i); // Then we assign the copy to the original // object currentObject.color = curColor; yield return null; } curColor = currentObject.color; /* ensure the fade is completely finished (because lerp doesn't always end on the exact value due to rounding errors) */ curColor.a = end; currentObject.color = curColor; } }
As you may recall from Chapter 1, 2D Twin-stick Shooter, we can use coroutines to pause functionality, yielding for a time and then resuming functionality. The IEnumerator
class holds the current state of the program and tells us where to continue. The yield return here is asking us to stop the function now and resume after a period.
Because coroutines are just functions, we can also have parameters in them, just as in the preceding function. With this in mind, we can also nest them together in order to have complex interactions and use our abstracted functions in multiple ways to create interesting behaviors.
CameraFlash
, as follows:IEnumerator CameraFlash() { yield return StartCoroutine(Fade(0.0f, 0.8f, 0.2f, cameraFlash)); yield return StartCoroutine(Fade(0.8f, 0.0f, 0.2f, cameraFlash)); StopCoroutine ("CameraFlash"); }
For more examples on how coroutines can be used, check out http://unitypatterns.com/introduction-to-coroutines/ and http://unitypatterns.com/scripting-with-coroutines/.
PhoneBehaviour
scripts' Update
function:if (cameraActive && Input.GetMouseButton(0)) { StartCoroutine(CameraFlash()); }
Photo Camera
object, assign the Camera Flash
object to the Camera Flash
variable.Looks like everything is in working order! We can look around with our camera, zoom in and out, and shoot pictures.