Added rechargeable shields instead of health

The last slide was to try messing with the game and adding more stuff. I did an attempt at changing health to shields so that they could recharge as well while the player isn’t hit to give the player a better fighting chance.

image

Added the above circle sprite (From Open Game Art) as a child to the player prefab.

image

Then I updated the Player.cs script to have these code changes:

    [SerializeField] int startingHealth = 500;
    [SerializeField] int health;
    [SerializeField] float healthRechargeRate = 0.5f; // How often (seconds) the shield recharge happens
    [SerializeField] int healthRechargeQuantum = 10; // How much health to add each recharge

    // Use this for initialization
    void Start()
    {
        shield = transform.Find("Shield").gameObject;
        SetupMoveBoundaries();
        health = startingHealth;
        // Added the line below
        InvokeRepeating("ChargeShields", healthRechargeRate, healthRechargeRate);
    }

    void Update()
    {
        Move();
        Fire();
        // Added the line below
        SetShieldLevel();
    }

    // The following methods are all new
    private void ChargeShields()
    {
        health += healthRechargeQuantum;
        if (health > startingHealth)
        {
            health = startingHealth;
        }
    }

    private void SetShieldLevel()
    {
        SpriteRenderer spriteRenderer = shield.GetComponent<SpriteRenderer>();
        Color temp = spriteRenderer.color;
        temp.a = CalculateShieldAlpha();
        spriteRenderer.color = temp;
    }

    private float CalculateShieldAlpha()
    {
        return ((float) health) / ((float) startingHealth);
    }

I had to do some google searches to figure out how to mess with the sprite alpha and how to call the function repeatedly. I saw some approaches to use Time.deltaTime in the update method to decide if the function needs to be called but the InvokeRepeating function seemed the neatest.

2 Likes

Also tried adding a “Death Blossom” (inspired by The Last Starfighter) weapon which fires lasers in all directions.

    private void FireDeathBlossom()
    {
        if (deathBlossomCharges <= 0)
        {
            return;
        }

        for (int angle = 0; angle > -360; angle -= 15)
        {
            GameObject laser = Instantiate(
                    laserPrefab,
                    transform.position,
                    Quaternion.identity) as GameObject;
            laser.transform.Rotate(new Vector3(0, 0, 1), angle);

            Vector2 baseVector = new Vector2(0f, projectileSpeed);
            baseVector = Quaternion.Euler(0, 0, angle) * baseVector;

            laser.GetComponent<Rigidbody2D>().velocity = baseVector;
            AudioSource.PlayClipAtPoint(projectileSound, Camera.main.transform.position, projectileSoundVolume);
        }
        deathBlossomCharges--;
    }
5 Likes

Hey man,
I wanted to try using your code there to learn from the game and it looks like
your not initializing the name shield in this code.

What do you initialize it as?
a bool?

If you look at the code where that var named ‘shield’ is being assigned to :

 shield = transform.Find("Shield").gameObject;

You see its being set to the ‘gameObject’ property which happens to be of type GameObject.
So the var shield too needs to be declared of type GameObject which you can do under the other vars declared just above the Start function .

private GameObject shield;

Privacy & Terms