Implement a Scope

In this tutorial, you will learn how to create a scope to make it easier to shoot enemies from father away.

Creating a Scope



Let's add a scope to our gun so we can zoom in on any object or enemy we are looking at.

First, find any PNG of a scope for your game, or create your own, make sure you can see through the image. You can also use the Scope image located in the Prefab folder. Then in the inspector of the image, make sure the PNG is a Sprite so we can use it in the scene. After that create an image in your Canvas, make sure you set the position to be (0, 0).



Now its time to create the script for the scope. Create a script called Scope in your Scripts folder. First, create some public variables to access our camera, scope, and crosshair GameObjects. This way we can make edits to them in the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scope : MonoBehaviour
{
    public GameObject playerCamera;
    public GameObject scope;
    public GameObject crosshair;

    void Update()
    {

    }
}
          


In the Update function put two if statements that detect the right-mouse button (or any other button). One should detect if the button is pressed down, and the other should detect when it goes up.

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {

        }

        if (Input.GetMouseButtonUp(1))
        {

        }
    }
          


We need to disable our crosshair and enable our scope image every time we press the button. Then when the button is released the scope should disappear while the crosshair will return to the screen.

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            scope.SetActive(true);
            crosshair.SetActive(false);
        }

        if (Input.GetMouseButtonUp(1))
        {
            scope.SetActive(false);
            crosshair.SetActive(true);
        }
    }
          


Now to get the scope to zoom in. We can do this by changing the field of view on the player's camera. The default for the field of view is 60, so lowering it to 30 (or any number below 60) will make it zoom in. We also do not want to be zoomed in forever, so remember to set it back to 60 once the button is released.

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            scope.SetActive(true);
            crosshair.SetActive(false);

            playerCamera.GetComponent<Camera>().fieldOfView = 30;
        }

        if (Input.GetMouseButtonUp(1))
        {
            scope.SetActive(false);
            crosshair.SetActive(true);

            playerCamera.GetComponent<Camera>().fieldOfView = 60;
        }
    }
          


That is all the code we need to create the scope. Now to add it to the game scene. Add the Scope Script to the Player object. Then drag the main camera in to the camera slot. After that, drag the image of the scope into the scope slot. Then finally drag the crosshair image into the crosshair slot.



Now you have a scope for your shooter game! Feel free to make the scope zoom in more or less, or add any other features.

Making Enemy Pathing AI



We already have an Enemy script and Prefab that looks for the player and deals damage to them on contact. However the enemy still does not know how to move, or track the player in any way. This means we need to add a NavMesh to the scene.

Building a NavMesh means to build a large plane on the terrain, that molds to the shape of the terrain. This then can be used to create pathing AI for enemies and other Non-Player Characters (NPCs).

In the project there is already a folder for all the NavMesh scripts. Create an empty game object and name it NavMesh. Then add the script NavMeshSurface to the object you just made. Then press the Bake button in the NavMeshSurface script to create the NavMesh in the scene.



Now the enemies can find and try to attack the player, and go around obstacles. However if you look closely in the scene the enemies try to avoid the area the player spawns and the boxes. We don't want them to avoid those places, so we need to tell the NavMesh to not avoid certain layers. To do this we need to go to the Include Layers dropdown menu, and uncheck the layers Player and Boxes. Then re-bake the NavMesh on the scene.

Make sure to press Bake again after you have made those changes.



Challenge: Expand your map

Now that you have a functional FPS, go ahead and use what you've learned from the other lessons to build a more interesting map. Remember to re-bake the NavMesh when you make changes to the terrain or the AI won't use your updated map.

Try adding the following:

More spawn points
A building
A bridge
A tunnel