It's time to shoot

The next step is to give the player the ability to shoot. In doing so, the player controller creates a bulletPrefab. We don't have a bulletprefab yet, but we will create it later in the chapter. However, we can create a variable to hold the prefab, so it can be changed later on in the Inspector, as the following:

    public GameObject bulletPrefab; 

Now, in the FixedUpdate() function, after all the lines we already have, we need to instantiate the bullet every time the player shoots. In this case, we are going to check whether the player presses the Fire1 button or the E key. If so, we just instantiate the bullet prefab at the same position as the spaceship and without any rotation applied, which is expressed through the admittedly fancy Quaternion.Identity argument (you can find out more about quaternions in Unity at the following links: https://unity3d.com/learn/tutorials/topics/scripting/quaternions and https://docs.unity3d.com/ScriptReference/Quaternion.html:

    void FixedUpdate() { 
        // [...] 
        //Check if the player has fired 
        if (Input.GetKeyDown(KeyCode.E) || Input.GetButtonDown("Fire")) { 
                //Create the bullet 
                Instantiate(bulletPrefab, transform.position, Quaternion.identity); 
        } 
    } 

This works just fine, but it can be improved. As we anticipated, right now the player can repeatedly press the E key and can shoot many bullets. We want in some way to limit this process. In order to achieve this, we need to write a bit more code.

First, we need two variables to store the reload time, which is how often the player can shoot, and the last time the player has shot, which is a private variable to check if the reload time has expired:

    public float reloadTime = 1.0f; 
    private float lastTimeShot = 0f; 

We have to change the code in the FixedUpdate() to reflect this change. So, inside the if statement, it checks if the player has shot, and we can nest another if to check whether the player can shoot or if it is too early (the reload time hasn't expired yet). We can do this by calculating with a subtraction how much time has passed since the last time the player shot and compare it with the reload time. If this results as greater than the reload time, then we need to update the last time the player shot and actually created the bullet. In the end, the code looks like the following:

    void FixedUpdate() { 
        // [...] 
        //Check if the player has fired 
        if(Input.GetKeyDown(KeyCode.E) || Input.GetButtonDown("Fire")) { 
            //Check if the player can shoot since last time the spaceship has fired 
            if(Time.time - lastTimeShot > reloadTime) { 
                //Set the current time as the last time the spaceship has fired 
                lastTimeShot = Time.time; 
 
                //Create the bullet 
                Instantiate(bulletPrefab, transform.position, Quaternion.identity); 
            } 
        } 
    } 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset