Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Implementation of power-up functionality

Discussion in 'Scripting' started by OkeyNik, May 2, 2019.

  1. OkeyNik

    OkeyNik

    Joined:
    Mar 14, 2017
    Posts:
    41
    I have a power-up (blue bullet), how can I realize that my character can shoot them?
    Also in the lower left corner of the screen, I have a circle and I want the picture/icon of this power-up to appear in this circle and the countdown starts inside the circle (the remaining time of red bullets will be indicated)
    FYI: I have a 2d shooter game

    UPDATE: I forgot to add - at that moment when there will be power-up shots, it is necessary that simple shots do not exist. By default, I have blue ammo.

    After I understand how to implement a power-up functionality, I will start adding not only different colors of icons but also buffs or other types of shooting (aside, arc and so on).
     
    Last edited: May 2, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @OkeyNik

    "I have a power-up (blue bullet), how can I realize that my character can shoot them?"

    I think you should break down all the things you want. Your pickup is a pickup an shooting is another thing I guess.

    You have a visual pickup, that probably has a collider/trigger. When your player collides with a pickup, pickup/player should inform your system that a pickup was picked. Then you can activate/create a new item/weapon/feature for you player matching the picked item. Your player's weapon can shoot whatever was defined in the pickup.

    Make pickup items have optional lifetime, and if current item has lifetime, update it each Update. When the lifetime counter is zero, swap back the default item.

    "Also in the lower left corner of the screen, I have a circle and I want the picture/icon of this power-up to appear in this circle and the countdown starts inside the circle"

    You could have an event that you invoke every time you reduce the lifetime of current pickup. Make the event pass current item data. Your UI element can then listen to this event, and update the view based on this.
     
    OkeyNik likes this.
  3. OkeyNik

    OkeyNik

    Joined:
    Mar 14, 2017
    Posts:
    41
    Here is screenshot(code): https://drive.google.com/file/d/1sU3vQWyHS3D43muo0p-JvZ-xwfrukvCL/view?usp=drivesdk
    Code (CSharp):
    1. public GameObject PlayerDefaultShip;
    2.     public GameObject BulletPosition01;
    3.     public GameObject BulletPosition02;
    4.  
    5.  
    6.     // Update is called once per frame
    7.     void Update ()
    8.     {
    9.  
    10.      
    11.         if(Input.GetKeyDown("space"))
    12.         {
    13.          
    14.             GameObject bullet01 = (GameObject)Instantiate (PlayerDefaultShip);
    15.             bullet01.transform.position = BulletPosition01.transform.position;
    16.  
    17.             GameObject bullet02 = (GameObject)Instantiate (PlayerDefaultShip);
    18.             bullet02.transform.position = BulletPosition02.transform.position;
    19.  
    20.         }
    Here is screenshot(Unity): https://drive.google.com/file/d/1Dn_l5dFjOf2_JrZ-w4ISdIUjLynqhsNf/view?usp=drivesdk

    So, here is my question: If I go back to my question about the blue bullet, how can I use it (power-up bullet(red bullet)) if I use the blue bullet prefab(default bullet)?
    How can I change this code? Or what should I do?

    So: My default bullet is - blue color, i want to make red bullet color(power-up), but i dont know how to implement this is in my code

    For the power-up i will have "PlayerBullet2"(red color) prefab.
     
    Last edited: May 2, 2019
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @OkeyNik

    please don't PM for coding assistance... I think it is best to keep discussion public, so everyone can benefit.

    "How can I change this code?"

    I don't see anything in your code for pickup or swapping weapons, which I described in my post. I only see that when you press space, you are going to instantiate both bullet01 and bullet02 and then you set positions for these.
    Also, for some reason you are calling your bullet "PlayerDefaultShip".

    "Or what should I do?"

    Try to first create pseudocode for your pickup logic and weapon system and how you can enable weapon a or b or whatever when needed. Then try to write some code, and if you get stuck, then ask more questions.
     
    Last edited: May 3, 2019