Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coins and other pickup items

Discussion in '2D' started by VRlunatic, Dec 4, 2019.

  1. VRlunatic

    VRlunatic

    Joined:
    May 6, 2017
    Posts:
    14
    I'm making a 2D game with coins and other pickup items. These are placed around the level manually just like in a game like Sonic the Hedgehog.

    When picked up, should I destroy the prefab instance or just deactivate it? I heard that destroying an object is CPU intensive, and I have a lot of coins that can be picked up quite rapidly, just like in Sonic.

    What if the coins respawn after a certain time, such as 30 seconds. Would that make any difference? I'm targeting mobile devices, and I want to be efficient. Thank you for your feedback.

    JG
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    You could just have them pooled in a List or such and then "disable" them by toggling off the sprite and trigger, for example. But I think it's first better to try make your game worth playing before starting to do pre-optimization.
     
  3. VRlunatic

    VRlunatic

    Joined:
    May 6, 2017
    Posts:
    14
    Thank you for your reply. I got the coins to work the way I want, but perhaps I will need to revisit this topic to optimize, as I am targeting the mobile platform. Pooling sounds like a good idea, although I haven't done it yet. I have purchased Core GameKit which includes PoolMaster, but haven't used it yet.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You could break your level down into segments with a box collider. Use OnTriggerStay2D() to determine which segment you are in. You can instantiate the coins that are only within the segment, disable their sprite renderer and colliders when touched, so if the player dies before the next segment, all you have to do is enable them again. If the player reaches the next segment, you can destroy the game objects since you are instantiating new ones in the new segment (Using OnTriggerExit2D() ).

    I'd just be sure to set your segment colliders inside of the old segment (Overlapping) so everything can instantiate before actually entering the next segment.
     
  5. VRlunatic

    VRlunatic

    Joined:
    May 6, 2017
    Posts:
    14
    Thank you, that's a really useful reply. Is the method you described something similar to what the SECTR COMPLETE 2019 asset does? I purchased it already but haven't used it yet. I didn't know if it's a good solution for my game as it's just a 2D game, but it does load/unload areas as needed, as far as I can tell. If it's a solution, I might just use it instead of trying to code it myself - I'm not great at coding so I use PlayMaker to create my game.
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I have zero knowledge about what you are using. My college professor once told me, about web design, using software that does everything for you is useful, but what happens when you want something else? What happens when something breaks? You need to know how to code and if you do, then you don't feel the need to rely on things you can design yourself ;)

    I stand behind my comment above. Create a GameObject, add a box collider, size it over the area of your level you wish to be the first segment. (Insuring the player can never fall out of it or jump out of it)

    You attach a C# script and the only code you need is this:

    Code (CSharp):
    1. OnTriggerStay2D(Collider2D col)
    2. {
    3. if(col.tag == "Player")
    4. {
    5. // Here you instantiate your objects within the segment
    6. }
    7. }
    8.  
    9. OnTriggerExit2D(Collider2D col)
    10. {
    11. if(col.tag == "Player")
    12. {
    13. // Here you destroy all objects within the segment
    14. }
    15. }
    There's really not much to it. :)
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Lemme give you an example. Let's pretend this is your game and I made red boxes representing colliders. You can see 3 segments labled 1,2,3.
    upload_2019-12-5_19-37-52.png

    When the player is inside segment 1, nothing exist in 2 or 3. Notice the overlap? That's to preload the next segment. Nothing exist outside of the current segment you are in. Now if you want things to remain the same when you re-enter the segments, all you need is to save the states in a file rather than destroying everything.
     
  8. VRlunatic

    VRlunatic

    Joined:
    May 6, 2017
    Posts:
    14
    Thanks for your advice, it's very appreciated.
     
    MisterSkitz likes this.