Zak Woolley
Zak Woolley

Reputation: 189

Unity: How would I go about creating a power up system?

I want to implement a power up system into my 2d platformer but instead of the power ups just offering extra speed or a health replenishment my power ups will be like totally different characters with different sprites, sizes and abilities (e.g inverting gravity, shooting lasers, climbing on walls, etc.).

At the minute i'm just going to try getting the player to change to a different power up. Does anyone have any idea how I could do this? I'm thinking destroy the player when they come in contact with the power up and then spawn a new player, with the new ability, in it's place but i'm not sure.

Upvotes: 0

Views: 2519

Answers (2)

Razzlez
Razzlez

Reputation: 556

I would say the most effective way is using an event system;

PowerUpScriptIdea: powerup will have a OnTriggerEnter() method inside of it, and if an object collides, this method will check if its the player, if it is, hide/destroy the powerup, but before doing so, send a message/event to the player that a powerup has been collected.

PlayerScriptIdea: will have powerup() method corresponding to event powerup, once that event is called, player will have speed+=200, or jump+=10, etc.

If you want to save memory, instead of destroying the powerup, hide the meshrenender of the powerup, and just move it to the next place it appears in, etc, so that way you can reuse the same powerup objects.

Upvotes: 1

haksist
haksist

Reputation: 229

Hi Zak if you want to sell only characters I think you need to keep prefabs for every character in Resources folder. When the player buy character you just need to replace current character's prefab path and load it from resources when needed.

For power ups like climbing or gravity reversing you need to write scripts for each powerup and attach to current character when player buy it. You can manage power ups like showed below.

abstract class PowerUp : MonoBehaviour
{
    public void activate();
}

class GravityRevert : PowerUp 
{
private Character character_;
void Awake()
{
    character_ = getComponent<Character>();
}

public void activate()
{
    if(character_.gravity > 0)
         character_.gravity = -character_.gravity;
}

}

If you want to sell sprites. You need to keep sprites in Resources folder and when the player buy cloth you just need to replace cloth path of current character.

Upvotes: 0

Related Questions