Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Day 91 of 100 Days of VR: Endless Flyer – Implementing the Multiplier Power-Up Effect

0.00/5 (No votes)
6 Feb 2019CPOL3 min read 1.6K  
In the previous post, we worked on adding our new score-multiplier power-up into our game. Today we’re going to continue to build upon that. I decided to make some changes, I know I’ve been calling it the score multiplier, but in retrospect, the score doesn’t really do anything for you.

In the previous post, we worked on adding our new score-multiplier power-up into our game. Today we’re going to continue to build upon that.

I decided to make some changes, I know I’ve been calling it the score multiplier, but in retrospect, the score doesn’t really do anything for you. I don’t intend to use it for anything else another then high score if at all, which would make this a non-desirable power-up. Instead, I decided to not only increase the score but to also increase the value of the coins when we pick them up.

In today’s post we’re going to continue off that by:

  • Implement the score multiplier effect along with the coin multiplier.
  • Increasing our buff duration.

Today will be a relatively short day, but as always, let’s get started!

Step 1: Add the Multiplier Effect to the Score and Coin

Step 1.1: Adding the multiplier effect to our ScoreManager

Surprisingly, the work needed to be able to use our new power-up isn’t too much work!

To implement our score multiplier power-up we just need to double the score we get every frame. Where do we put that code? In our ScoreManager script of course!

Here’s what it looks like:

using UnityEngine;

public class ScoreManager : MonoBehaviour {

    public static ScoreManager Instance;

    private float _score = 0;

    void Start()
    {
        if (Instance != null)
        {
            // If Instance already exists, we should get rid of this game object
            // and use the original game object that set Instance   
            Destroy(gameObject);
            return;
        }

        // If Instance doesn't exist, we initialize the Player Manager
        Init();
    }

    private void Init()
    {
        Instance = this;
        _score = 0;
    }

    void Update()
    {
        // increase our score and then update our ScoreText UI.
        float increaseTime = Time.deltaTime * 10;
        if (PlayerManager.Instance.ContainsPowerUp(PlayerManager.PowerUpType.Score))
        {
            increaseTime *= 2;
        }
        _score += increaseTime;
        GameUIManager.Instance.SetScoreText((int)_score);
    }
}

Walking through the code

Here’s what we did in the code:

  1. In Update() we modify our original score increase code by saving the value that we increased in another variable called increaseTime, we check to see if the player has picked up the Score PowerUpType and if they did, we double our float value. Then we add it to our score.

And that’s it! We have now implemented our score-multiplier power-up.

Now at this point, everything works nice and dandy, however, if we were to try and change the multiplier effect, then we might run into problems. Currently, we have hard-coded the value to be 2X, but what if we want it to be 3X, 4X, or 5X, but worry not we will come back and look at this problem.

Step 1.2: Add the Coin Multiplier Effect in GameManager

Now that we implemented the score multiplier, we need to do something similar with our coin.

We increase our coin count in GameManager, so let’s add our script there. Here’s what our GameManager looks like:

using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    private int _coin = 0;

    void Start ()
	{
	    if (Instance != null)
	    {
	        // If Instance already exists, we should get rid of this game object
	        // and use the original game object that set Instance   
	        Destroy(gameObject);
	        return;
	    }

	    // If Instance doesn't exist, we initialize the Player Manager
	    Init();
	}

    private void Init() {
        Instance = this;
        _coin = 0;
    }

    // Called from outside function for when the player collects a coin.
    public void CollectCoin()
    {
        int scoreIncrease = 1;
        if (PlayerManager.Instance.ContainsPowerUp(PlayerManager.PowerUpType.Score))
        {
            scoreIncrease *= 2;
        }
        _coin += scoreIncrease;
        GameUIManager.Instance.SetCoinText(_coin);
    }

    // Return the number of coins that we have collected.
    public int GetCoin() 
    {
        return _coin;
    }
}

Walking through the code

Like what we have done with ScoreManager:

  1. When CollectCoin() is called, we create a variable to hold our default increase value (1) and then if we have our power-up active, we multiply that value by 2. Finally, with our value, we increase our _coin

At this point, we have a similar problem to what we discussed with our Score for when we ever want to change the values, but for now, we’re going to leave this as a problem for another day.

Step 1.3: Increase the buff duration

On a separate note, something I realized that our power-ups don’t last too long.

I’ve been playing around with them, so I don’t remember their original value, but I decided that making each power-up last 45 seconds would be a better change.

Currently, our power-up time is set by our PlayerManager script, let’s change that value to be 45 seconds.

public class PlayerManager : MonoBehaviour
{
  …
  private float powerUpDuration = 45f;
  …
}

We just changed the value to be 45 seconds instead of whatever low value we had before. Now our power-ups will last much longer!

End of Day 91

That’s it and that’s how we would go about adding a score multiplier to our game.  It might not be the best way, but it gets the work done!

To recap, today we:

  1. Increased the score we generated when our power-up is active
  2. Increased the coin we get when our power-up is active
  3. We changed our power-up duration to last longer

At this point, we have the implementation of our power-up done, in the final post in this series, we’re going to add the last visual and sound effects to the game. I’ll see you all then!

Day 90 | 100 Days of VR | Day 92

Home

The post Day 91 of 100 Days of VR: Endless Flyer – Implementing the Multiplier Power-Up Effect appeared first on Coding Chronicles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)