Skip to content

Adding Escort Upgrades

John Nguyen edited this page Sep 3, 2020 · 2 revisions

Adding Escort Upgrades

Escorts are a weapon upgrade that fly around the player and help deal damage.

Prerequisites:

  • Created a sprite for the escort
  • Create a sprite for the escort in the UI (Check other UI sprites to see how yours should look like).

Creating a Prefab

  1. Open any Level scene. On the file hierarchy on the left, right-click and Create Empty. alt text

  2. Rename the new GameObject to your escort name. (IMPORTANT This name will be referenced multiple times in this tutorial, REMEMBER it!). Then, drag and drop a png or jpeg image of your escort sprite into the Sprite folder. (Change the Texture Type to Sprite if necessary). Finally, drag and drop your sprite onto your GameObject. alt text

  3. Drag and drop your GameObject into the Resources folder. Delete the GameObject from the scene. I will call this GameObject a Prefab from now on. alt text

  4. Double-click on the your Prefab in the Resources folder to open the Prefab editor. In the Inspector, click Add Component and select New Script. Create the script and remember to move the script to the Scripts/Weapons/Escorts folder for organizational purposes. alt text

Coding

  1. Open your escort script. Have your class inherit from the Escort class and delete the Start() and Update() functions. Your code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lancaster : Escort
{
    
}
  1. For this tutorial we will override the Attack() function from the Escort class. This function will be called every time the player shoots their Gun. I want my escort to shoot a PlayerBomb projectile. Here's my code:
// This GameObject will be assigned through the Unity Inspector and Instantiated in the Attack() function.
    public GameObject bombPrefab;

    public override void Attack()
    {
        // Instantiate the PlayerBomb prefab
        GameObject go = Instantiate(bombPrefab, transform.position, Quaternion.identity);

        //Get the PlayerBomb script from the GameObject we just instantiated
        PlayerBomb bomb = go.GetComponent<PlayerBomb>();

        //Shoot the bomb up
        bomb.targetVector = new Vector3(0, 1, 0);

        //Set the speed of the bomb
        bomb.speed = 2;

        //The bomb will detonate and produce 5-9 PlayerBullet projectiles
        bomb.numberOfBullets = Random.Range(5, 10);
    }

Your escort does not have to shoot a PlayerBomb projectile like mine. You can instantiate a different project or not shoot one at all (Sword escort). Be creative with your escort!

Optional Override the Update() function if you want your escort to move in a different way.

Finishing your Prefab

  1. Check for any compiling errors and go back to your Prefab editor and edit your script public variables in the inspector on the right side.
  • Rotate Speed: How fast the escort will rotate around the player
  • Radius: How far away the escort will rotate around the player
  • Attack Speed: How many seconds until your escort will executes the Attack() function.
  • Bomb Prefab: I will drag and drop the PlayerBomb prefab from the Prefab folder into this public variable. (This will change depending on how you implemented your escort script).

alt text

Adding your Upgrade to the Upgrade Menu

  1. Open the Upgrade scene from the Scene folder. Open the Canvas in the file hierarchy. Add your UI sprite into the Sprite folder.

alt text

  1. Open the Canvas hierarchy and click on Gun Tree. Hide the Gun Tree by clicking on the checkmark next to the name. Select the Escort Tree and display it by clicking on the checkbox next to the name.

alt text

    • Duplicate one of the toggles (Either Squadron or Sword) and move the duplicated toggle to a new position. (It is important that you get this step correct or very bad things will happen).
    • *** IMPORTANT *** Name the toggle "EscortName" + "Number".
      • "EscortName" is the name of your Prefab from the previous steps.
      • "Number" is how many of these escorts will spawn around the player.
    • Open the text "EscortName"Text GameObject. Rename it and change the text.
    • Open the Background GameObject. Change the Source Image to your UI escort sprite.
    • Duplicate one of the arrows and point to your new toggle.

alt text

    • Open the SpriteManager prefab in the Prefab folder.
    • Modify the size of the array in the inspector to add your sprite.
    • *** IMPORTANT *** Name the new array element as the same name as your Toggle from Step 10. (In this case "Lancaster1").
    • Drag and drop the same UI Sprite from Step 10.

alt text

Coding the Upgrade UI

    • Open the UpgradeMenu.cs script in Scripts/UI.
    • Navigate to the bottom of the script in the TreeButton region.
    • Create a new function for your toggle. Mine will be called SingleLancasterSelect()
    • Copy the code from any of the Select functions and modify. The lines with comments will be the lines you will modify.
public void SingleLancasterSelect()
    {
        // Make sure that the integer in the next 3 lines are not used in other functions and are the same in this function. Failure to do this will cause weird Toggle issues
        toggles[22] = toggles[22] ? false : true;
        SetAllSelectToFalse(22);
        if (toggles[22])
        {
            //For upgrading escorts, set this variable to true
            escortSelected = true;
            Info.SetActive(true);
            // Set the name of your Escort upgrade
            Info.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = "Lancaster";
            // Set the description of your Escort upgrade
            Info.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = "A bomber escort will rotate around the player and shoot bombs. The bomb will explode if the player's mouse is below the bomb projectile.";
            // Find your UI Sprite from the SpriteManager by name
            Info.transform.GetChild(3).GetComponent<Image>().sprite = FindObjectOfType<UISpriteManager>().Find("Lancaster1");
            // Set the cost text of your Escort upgrade
            Info.transform.GetChild(4).GetComponent<TextMeshProUGUI>().text = "Cost: 2000";
            // Set the Name of your Escort upgrade. **Important** make sure it is the same as your Toggle name
            powerupName = "Lancaster1";
        }
        else
        {
            escortSelected = false;
            Info.SetActive(false);
        }
        //The next two numbers will be the cost of your upgrade in points
        DisableUpgradeButton(2000);
        upgradeCost = 2000;
        DisableEquippedButton(data);
    }
    • Fix any compile errors and return to the Upgrade scene and click on your Toggle in the file hierarchy.
    • In the Inspector, change the On Value Changed (boolean) function to the function that you written in Step 12. alt text
    • Click on the Canvas GameObject in the file hierarchy. In the Inspector on the right side, navigate to the Escort Tree public variable.
    • Modify the size of the array to add your upgrade.
    • On the array element that you added, change the "Name" variable to the same name as your Toggle or powerupName variable. (In this case Lancaster1).
    • Leave the Powerup number as 0.
    • Modify the Children array. If your upgrade has Children (Upgrades that require this upgrade) modify this array and copy the names of the child upgrades into each element. In this case, Lancaster1 has no children (It is at the end of the tech tree) so I set the size to 0. Check how I set the children in other upgrades as an example on how to set children.
    • Modify the Children array of the Parent Upgrade (The upgrade that precedes your upgrade). In my case, I am going to modify the Squadron 3 Children array. Add the name of your upgrade to this array.

alt text

Finishing Touches

  1. Test your escort! If it doesn't work, submit an issue on the Issues board.