DEV Community

Cover image for Prototype: Boost Powerup

Prototype: Boost Powerup

Justin Horner
◾️ Game Programmer 👾🕹 ◾️ C#/.NET Developer 📱 ◾️ Musician 🎸 ◾️ Software Engineer @grovecollab 🌿 ◾️ Opinions are my own
Originally published at ・2 min read


In the last post of the main series, we added a small collectable system that will allow us to quickly prototype some ideas. In this post, we'll create our first powerup for the player to collect.

The Challenge

The challenge is to create a new powerup collectable prefab that will change the player's movement speed for a short duration, and then reset back to the initial speed.

Let's get started.

The Prefab

I'll base the Boost Powerup on our Collectable prefab by duplicating it and renaming to Boost Prefab. Now, I want to set the Collectable Type of this prefab to Boost, which we currently don't have. Let's add Boost to the enum.

public enum CollectableType
Enter fullscreen mode Exit fullscreen mode

Now I'll set the type property in the inspector.
collectable type in inspector

Speed Boost

Inside the Player script, I'll check against the collectable type and implement a speed boost. I'll create a separate private method that will activate all future collectables to isolate the behavior from OnTriggerEnter, as a personal preference to keep the message method clean and simple.

I made sure that the prefab had a tag set to Collectable to identify it. I prefer to create constant strings, so you'll notice the use of my static Constants.cs class to access Tag.Collectable, which simply holds the string value of the tag name "Collectable".

private void OnTriggerEnter(Collider other)
    if (other.CompareTag(Constants.Tag.Collectable))
Enter fullscreen mode Exit fullscreen mode

The ActivateCollectable method takes the collectable object that the player collided with and applies the effects to the player. For now, we just have boost, which increases the speed by value from the constants file Collectable.BoostAmount.

private void ActivateCollectable(Collectable collectable)

    switch (collectable.Type)
        case CollectableType.Boost:
            _speed *= Constants.Collectable.BoostMultiplier;
            StartCoroutine(CollectablePowerdownRoutine(collectable.Duration, UndoBoost));
Enter fullscreen mode Exit fullscreen mode

Power Down

This accomplishes the goal to boost speed, but we also need a way to undo or power down the effect. To do this, I'll provide a method for the coroutine to execute once the wait time has elapsed.

First, we need to add a duration for the effect. I'll do that by adding a field and public get accessor to Collectable.cs. If you're unfamiliar with the read-only property syntax, see expression-bodied members.

public class Collectable : MonoBehaviour
    [SerializeField] private float _speed = 5;
    [SerializeField] private float _duration = 5;
    [SerializeField] private CollectableType _type;

    public float Duration => _duration;
    public CollectableType Type => _type;

Enter fullscreen mode Exit fullscreen mode

Now I'll add the coroutine to power down the effects based on this duration.

private IEnumerator CollectablePowerdownRoutine(float duration, Action resetDelegate)
    yield return new WaitForSeconds(duration);
Enter fullscreen mode Exit fullscreen mode

Here's the result
boost in action


That's it! We now have a boost powerup the player can collect with a duration that can be easily set by designers.

Stay tuned for the next post.

Take care.

Discussion (0)