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.
Added the above circle sprite (From Open Game Art) as a child to the player prefab.
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.