0
\$\begingroup\$

In my game there is a bird flying and on touch it throws different artifacts projectile downwards. And these artifacts should hit the containers moving right to left on the screen. Now if user keeps on tapping rapidly it make a line of artifacts and it didn't miss a single container. So what I want to do is to restrict user to throw 3 artifacts at a time and when one of them got destroyed it'll make room for one more. Here is how I'm doing it:

In my player.cs I'm using a static int named inventory and then in Update() method I've the code where it increment the inventory.

if (Input.GetTouch(i).phase == TouchPhase.Began){
                OnShoot ();
                inventory++;
                Debug.Log ("Inventory: " + inventory);
            }

and in OnShoot() I'm checking if (inventory < 4) then Instantiate otherwise if (inventory > 4) set inventory = 0;.

Now in the artifacts.cs if it hit the container and get destroyed or got destroyed going out of the bound missing the container it should Player.inventory--;

This code works fine but if the user speed of tapping is more then it again started throwing like in a line form as described above.

\$\endgroup\$
  • \$\begingroup\$ Well.. probably resetting inventory = 0 in OnShoot is a mistake? It's kinda hard to answer without the relevant bits of code though. \$\endgroup\$ – bummzack Mar 25 '15 at 10:47
0
\$\begingroup\$

First of all, you should stop incrementing inventory, if you don't spawn more projectiles.

Example:

if (Input.GetTouch(i).phase == TouchPhase.Began){
    if(inventory < 3){ // spawn 3 at max
        OnShoot ();
        inventory++;
    }
}

Then decrease inventory whenever a projectile gets destroyed. Eg.

void OnTriggerEnter2D(Collider2D other) {
    if(Player.inventory > 0){
        Player.inventory--;
    }
}

Your OnShoot should no longer perform any checks against inventory, but just spawn a projectile instead...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.