Instantiate GameObject at the speed of user touch or click

by Kartik Shah   Last Updated March 30, 2017 07:13 AM

I am working on a game and when I touch the screen it instantiates GameObject as per my current code but this sometimes instantiate fewer GameObjects and my player crosses the path which is not even instantiated so what I want to do is instantiate the GameObject as per the rate of Click or touch from the user.

IEnumerator GenerateNewPath()
    {
        while (true) {
            if (Input.GetMouseButtonDown (0)) {

                if (noOfClick == 0) {
                    randomNumber = Random.Range (1, 5);
                    noOfClick = Random.Range (10,12);
                }

                if (randomNumber == 1)          
                    startPosition = LeftTurn ();
                else if (randomNumber == 2)     
                    startPosition = RightTurn ();
                else if (randomNumber == 3)     
                    startPosition = Straight ();
                else if (randomNumber == 4)     
                    startPosition = Reverse ();

                randomTile = Random.Range (0, 8);

                if (randomTile == 2 || randomTile == 5) {
                    if (Random.Range (0, 8) == 2) {
                        temporaryTile = Instantiate (tile, startPosition, Quaternion.identity);
                        temporaryTile.transform.SetParent (transform);
                        //
                        startPosition = new Vector3 (startPosition.x, startPosition.y + (height / 3), startPosition.z);
                        tileSpriteRenderer.sortingOrder = tileSpriteRenderer.sortingOrder + 1;
                        temporaryTile = Instantiate (tile, startPosition, Quaternion.identity);
                        temporaryTile.transform.SetParent (transform);
                        tileList.Add (temporaryTile);

                    } else if (Random.Range (0, 8) == 5) {
                        temporaryTile = Instantiate (tile, startPosition, Quaternion.identity);
                        temporaryTile.transform.SetParent (transform);
                        tileList.Add (temporaryTile);
                        //
                        startPosition = new Vector3 (startPosition.x, startPosition.y - (height / 3), startPosition.z);
                        tileSpriteRenderer.sortingOrder = tileSpriteRenderer.sortingOrder - 1;
                        temporaryTile = Instantiate (tile, startPosition, Quaternion.identity);
                        temporaryTile.transform.SetParent (transform);
                    }
                } else {
                    temporaryTile = Instantiate (tile, startPosition, Quaternion.identity);
                    temporaryTile.transform.SetParent (transform);
                    tileList.Add (temporaryTile);
                }
                Debug.Log (tileList.Count);

            }
            yield return null;
        }
    }

This is my current code. this code instantiates GameObject when I click on it but not able to instantiate faster as user click speed.



Related Questions



Touch a specific area on the screen in Unity 5

Updated February 25, 2017 22:13 PM

Unity - Touch Input help needed

Updated July 09, 2017 03:13 AM