Plan du site  
pixel
pixel

Articles - Étudiants SUPINFO

Creating a PowerUp system in Unity

Par Jean-Baptiste AMMIRATI Publié le 19/09/2019 à 18:04:14 Noter cet article:
(0 votes)
Avis favorable du comité de lecture

Presentation

Hello everyone, I will quickly and simply present how to set up a powerUp system in your game creation via Unity. But on top of that I'm going to you add a particle effect to add a touch of realism.

Executive Summary

  • Creating the PowerUp via GameObject

  • Creating the script

  • Assigning a particle effect

  • Conclusion

Creating the PowerUp via GameObject

Creating a PowerUp will probably seem simple but it can exist different ways. In this tutorial, we will have a character who will be able to move, or right-click and import the packages that Unity provided natively or you can simply create a script that will allow you to to move your character to you to choose. In my case, my character is a cube or I assigned the tag "Player" with a "Plane" where our character can be move. Then let's create our boost, we'll make sure that once our character takes the PowerUp and increases its size. To do this we will create a sphere that is renamed "PowerUp" and in the "Mesh Collider" box it is necessary imperatively check "Is Trigger" otherwise your character will have a collision with our boost but there will be no effect. Once this is done, let's move on to the script.

Creating the script

The script is rather short but effective, we will have to declare our magnitude multiplier via this line of code:

public float multiplier = 1.4f;

Then we will create a new function that will call another one in order to purify our code. This function will allow that once the character is in contact with the boost then we call a new function that will perform our action on the character. the first function will be:

public void OnTriggerEnter(Collider other)
 {
            if(other.CompareTag("Player"))
            {
                     Pickup(other);
             }
 }

// La fonction nommé Pickup :
public void Pickup(Collider player)
 {
            Debug.Log("Power up is picked !");
            // Ca va augmenter la taille du joueur une fois qu'il prends le powerUp !        
            player.transform.localScale *= multiplier;        
            Destroy(gameObject);        
 }   

This function thanks to the multiplier will increase the size of our character one time the boost taken and then destroy the gameObject afterwards. You can now check that all this works and see the result for yourself!

Exemple :

Before

After

Assigning a particle effect

Let's add a little realism once you take the boost, to do this he you'll have to go to the Asset Store directly to Unity. But first you will have to create a account in go via Google Chrome, FireFox, etc. onhttps://assetstore.unity.com/, fill in the different information so that your account is valid for download an asset.

Once your account has been validated, look for a particle effect that you like for my part it will be this one:

https://assetstore.unity.com/packages/vfx/particles/fire-explosions/fire-explosion-vfx-48795

Once you have imported the package you just have to modify it very slightly the code previously, for that we will add this line of code:

public GameObject pickupEffect;

Which will allow us to assign a gameObject that will do this effect Wow!

Then let's add this again so that once the character is boosted, the animation is triggered like the picture above.

Instantiate(pickupEffect, transform.position, transform.rotation);

I give you the whole code so that you just have to copy and paste without too much think about it :)

  using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PowerUp : MonoBehaviour
{
         public GameObject pickupEffect;
         public float multiplier = 1.4f;

         public void OnTriggerEnter(Collider other)
        {
                   if(other.CompareTag("Player"))
                  {
                        Pickup(other);
                   }
         }

 public void Pickup(Collider player)
        {
                   Debug.Log("Power up is picked !");
                   Instantiate(pickupEffect, transform.position,
            transform.rotation);
                   // Ca va augmenter la taille du joueur une fois qu'il prends le powerUp !
                   player.transform.localScale *= multiplier;
                   Destroy(gameObject);
           }
}

Conclusion

And that's it for this tutorial, we quickly saw how to create a PowerUp without too much difficulty you can do this to improve the health of your person if your game is more for an SPF or other. I hope this is what you're looking for. will have more in any case me I made a pleasure of writing this for you, I tell you to soon for new little tutorials like this.

A propos de SUPINFO | Contacts & adresses | Enseigner à SUPINFO | Presse | Conditions d'utilisation & Copyright | Respect de la vie privée | Investir
Logo de la société Cisco, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société IBM, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sun-Oracle, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Apple, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sybase, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Novell, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Intel, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Accenture, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société SAP, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Prometric, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Toeic, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo du IT Academy Program par Microsoft, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management

SUPINFO International University
Ecole d'Informatique - IT School
École Supérieure d'Informatique de Paris, leader en France
La Grande Ecole de l'informatique, du numérique et du management
Fondée en 1965, reconnue par l'État. Titre Bac+5 certifié au niveau I.
SUPINFO International University is globally operated by EDUCINVEST Belgium - Avenue Louise, 534 - 1050 Brussels