Polishing the UX of a 2D Space Shooter Player Script (Part 2: collectables)
This section describe two examples of player enhancements as collectable game objects that appear in a random spawn time between 15 and 20 seconds. Each provide items that must be gathered to survive the waves of enemies: a health pack to avoid game over, a laser refill to keep destroying enemies.
- Ammo Collectable
- Health Pack
These collectables work like the power ups, through a trigger collider that identify the player and call a method inside the player script.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Player player = other.transform.GetComponent<Player>();
AudioSource.PlayClipAtPoint(_powerUpClip, transform.position);
if (player != null)
{
switch(collectibleID)
{
case 0:
player.ReloadAmmo();
break;
case 1:
player.Heal();
break;
}
}
Destroy(this.gameObject);
}
}
The methods are simple. The Heal() method adds one integer value to the player’s _currentHealth, updates the UI manager and toggle off the damage flares that turn on through the damage method. The ammo method sets the _currentAmmo value back to it’s maximum value, change the color and visibility of the ammo indicator and set the bool for shooting back to true. This is how it looks on the Player Script:
public void Heal()
{
if(_currentHealth != 3)
{
_currentHealth++;
_uiManager.UpdateLives(_currentHealth);
}
if(_currentHealth == 2)
{
_wFL.SetActive(false);
}
else if( _currentHealth == 3)
{
_wFR.SetActive(false);
}
}
public void ReloadAmmo()
{
_currentAmmo = maxAmmo;
_ammoIndicatorRenderer.color = Color.green;
_ammoIndicator.SetActive(true);
_isAmmoReady = true;
}
This adaptation allows the user experience to feel more meaningful while running the same combat loop over and over. The game is reaching that experience I’m looking for.