Click here to Skip to main content
15,889,909 members
Articles / All Topics
Technical Blog

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

Rate me:
Please Sign up or sign in to vote.
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)


Written By
United States United States
Joshua is a passionate software developer working in the Seattle area. He also has experience with developing web and mobile applications, having spent years working with them.

Joshua now finds his spare coding time spent deep in the trenches of VR, working with the newest hardware and technologies. He posts about what he learns on his personal site, where he talks mostly about Unity Development, though he also talks about other programming topic that he finds interesting.

When not working with technology, Joshua also enjoys learning about real estate investment, doing physical activities like running, tennis, and kendo, and having a blast with his buddies playing video games.

Comments and Discussions

 
-- There are no messages in this forum --