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

2D Tap and destroy game

Discussion in 'Editor & General Support' started by Kosidinma, Jul 29, 2014.

  1. Kosidinma

    Kosidinma

    Joined:
    Aug 1, 2013
    Posts:
    25
    Hi,
    I am trying to create a game where a game object is instantiated in 2d space, if clicked, it will change texture, and if not, it will disappear. I have managed to cover most of it but my main issue is the fact that for the period when the game object is visible, I can't tap it except if I somehow got it at the exact frame it was spawned. Please does anyone have any idea on how I can make the game object "tapable" for the entire time its visible?
    A snippet of my code:
    Code (JavaScript):
    1. #pragma strict
    2. var prefeb : GameObject[];
    3. var Spawnpoint : Transform[];
    4. private var prefeb_num;
    5. private var tele_num;
    6. private var clone : GameObject;
    7. private var timeLimit = 3.5; //accelaration time ie..time before speed increase
    8. private var waitingtime = 1.6; //time before sprite disappears
    9. var tappedSprite : GameObject;//sprite was tapped
    10. var missedSprite : GameObject;//sprite was not tapped
    11. private var tapped = false;// initial
    12. private var elapsedtime : float = 0f;
    13. private var destruction; //check if time to destroy prefab
    14.  
    15. function Start () {
    16. tele_num = Random.Range(0,10);
    17. prefeb_num = Random.Range(0,5);
    18. clone = Instantiate(prefeb[prefeb_num], Spawnpoint[tele_num].position, Quaternion.identity );
    19. Destroy (clone, waitingtime);
    20. WaitAndDestroy();
    21. while (1) //i'm using a while loop in start instead of an update function as update doesn't do well with delays
    22. {
    23. yield WaitForSeconds(waitingtime);
    24. Spawn();
    25. WaitAndDestroy();
    26. }
    27. }
    28.  
    29. function Update () {
    30.  
    31. }
    32.  
    33. function Spawn() //spawn at one of my spawn points
    34. {
    35. tele_num = Random.Range(0,15);
    36. prefeb_num = Random.Range(0,5);
    37. clone = Instantiate(prefeb[prefeb_num], Spawnpoint[tele_num].position, Quaternion.identity );
    38. Destroy (clone, waitingtime);
    39. //elapsedtime = Time.time + waitingtime;
    40. }
    41.  
    42. function WaitAndDestroy(){
    43. if(Time.time >= timeLimit)
    44.     {
    45.      // remove 0.18 from the time...
    46.      waitingtime -= 0.1;
    47.      if (waitingtime <= 0.3){
    48.      waitingtime = 0.3;}
    49.      // make it wait another 3.5 seconds...
    50.      timeLimit += 3.5;
    51.     }
    52.     checktap();
    53.     if (tapped) //when tapped
    54.     {
    55.     Destroy (clone); //destroy
    56.     var tappedclone = Instantiate(tappedSprite, Spawnpoint[tele_num].position, Quaternion.identity ); //put the good sign at the same position
    57.     yield WaitForSeconds(0.1); //wait small
    58.     Destroy (tappedclone);//destroy the good sign
    59.     tapped = false;
    60.     }
    61.    
    62.   /*  if (!tapped && destruction ) //no tap + time up
    63.     {
    64.    //yield WaitForSeconds(waitingtime); //wait
    65.    Destroy (clone); //destroy
    66.    var missedclone = Instantiate(missedSprite, Spawnpoint[tele_num].position, Quaternion.identity ); //bad sign instead
    67.    yield WaitForSeconds(0.1); //wait small
    68.    Destroy (missedclone);// destroy
    69.    }
    70.  
    71.    if (!tapped && !destruction ) //no tap + time left
    72.     {
    73.    checktap();
    74.    } */
    75. }
    76.  
    77. function checktap()
    78. {
    79. if ((GameObject.FindWithTag("Slyer")))
    80. {
    81. if (Input.GetKeyDown ("space"))
    82. {tapped = true;
    83. Destroy (clone); //destroy
    84. var tappedclone = Instantiate(tappedSprite, Spawnpoint[tele_num].position, Quaternion.identity ); //put the good sign at the same position
    85. yield WaitForSeconds(0.1); //wait small
    86. Destroy (tappedclone);//destroy the good sign
    87. }
    88. if (!Input.GetKeyDown ("space"))
    89. tapped = false;
    90. }
    91. }
    92.  
    Thanks for any help :).
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Your code seems confusing, I can't make sense of your logic.

    You spawn an object, then destroy it 3 seconds later. Then you wait and destroy, only to spawn it again.

    Why use while(1)? While will always run, as 1 can never not be one. Then inside that you ep yield and wait for seconds, to perform waitanddestroy, something you have already called.


    Sloppy.



    Can you explain better your issue?


    How are you running input? As you are saying tap must be precise at some point.
     
  3. Kosidinma

    Kosidinma

    Joined:
    Aug 1, 2013
    Posts:
    25
    I spawn random objects (tagged "slyers") at random spawnpoints one at a time. Once spawned, they should last for 3 seconds, if the user hasn't tapped spacebar, they should disappear and another random object is spawned which also has the same lifespan. If the spacebar is tapped, it should change the game object by destroying it and instantiating another object (not a "slyer" this time) at the exact position the object was. This new object should only be visible for about half a second or so. The issue is that the spacebar tap doesn't seem to be detected during the lifespan of the object but only at the exact moment its spawned which is almost impossible to catch.
     
  4. Kosidinma

    Kosidinma

    Joined:
    Aug 1, 2013
    Posts:
    25
    Kind of like a 2d fruit ninja. But instead of the fruits flying up and down, I want each fruit just appear on the screen for a period of time;
    If spacebar is tapped within that time period, it should divide.
    If its not tapped, it should get rotten.
    Then there should be a short wait; then the next fruit should appear and the cycle continues.
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So do you have a time frame for when fruit are available to slice? Ex, fruit appear, a random amount, in random positions, they stay there for x time, then are removed, (if they have not been destroyed by the user), then the steps are taken again?

    Edit.
    If so, and since your problem seems to be that the items can only be tapped at spawn, somehow, your space bar input is losing track of the items.

    I would do this, look into...
    Pooling, pool instances of the fruit.
    List, have two lists, one for active fruit, in scene and inactive out of scene.
    Have a manager. This manager will pull inactive fruit from the pool list. Remove any selected fruit, from the inactive list, add to active.
    During the window of time when fruit are available, if space bar hit, (here is where you lose me because how do you know which fruit you hit?) but, if space bar pressed, any active fruit, is removed from scene, removed from active list, added to inactive.
     
    Last edited: Jul 29, 2014
  6. Kosidinma

    Kosidinma

    Joined:
    Aug 1, 2013
    Posts:
    25
    It doesn't matter for now which fruit was hit because I assume they are all just fruits but with different textures. Your method seems reasonable. Please how do I go about implemeting that in code? Thanks for the help :).
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hm
    Tricky thing you ask, becuase if your asking you likely have no idea to do something, kind of simple but very fundamental, to most gameplay.

    Go over my edited post from above.
    You need to look up, pooling, Lists, and input.


    Code (csharp):
    1.  
    2. //mng
    3. public List<itemScript> items;
    4.  
    5. void gatherItemsForScene(){
    6. int itemsCount = items.Count;
    7. for (int i = 0; i < itemsCount; i++){
    8. items[i].loadIntoScene();
    9. }
    Code (csharp):
    1.  
    2. //itemScript
    3. public void loadIntoScene(){
    4. itemsMng.items.Remove(this);
    5. itemsMng.activeItems.Add(this);
    6. // now set pos, renders etc.
    7.  

    What you have done is have a manager run thru all its items in a list and inform each to load into scene. Upon doing so, this item removes itself from the mngs items list and adds itself to an active items list. This is a way to know which items are in scene. Of course if all your items are inScene, each time, then there is no need.

    You will then need to monitor input and remove items that are active appropriately. When you remove an item from scene remove it from the acticeItems list and ad it to the idle, items list. Now, this item can be recycled.
     
    Last edited: Jul 30, 2014
    Kosidinma likes this.
  8. Kosidinma

    Kosidinma

    Joined:
    Aug 1, 2013
    Posts:
    25
    Thank you so much for the help