Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit)

I finally got the idea random spawn going, but now need help in wanting the spawning prefabs located within a rectangle area. Here are ideas of want I mean from these web links.  how to create a spawn point within shape area for unity - Google Search 

Also here is the random spawn script that I made if needed to apply where for the rectangle script to go to. 

public GameObject objectToSpawn;

    public GameObject parent;

    public int numberToSpawn;

    public int limit = 15;

    public float rate;

    float spawnTimer;

    // Start is called before the first frame update

    void Start()

    {

        spawnTimer = rate;

    }

    void Update()

    {

        if (parent.transform.childCount < limit)

        {

            spawnTimer -= Time.deltaTime;

            if (spawnTimer <= 0f)

            {

                for (int i = 0; i < numberToSpawn; i++)

                {

                    Instantiate(objectToSpawn, new Vector3(this.transform.position.x + GetModifier(), this.transform.position.y + GetModifier())

                        , Quaternion.identity, parent.transform);

                }

                spawnTimer = rate;

            }

        }

    }

    float GetModifier()

    {

        float modifier = Random.Range(-10.5f, 10.5f);

        if (Random.Range(0, 2) > 0)

            return -modifier;

        else

            return modifier;

    }

}

Hello again...

Ok, a few things...

- Your timer tries to work in a way that it will create 'limit' gameobjects every 'rate' seconds, non-stop. And the only condition to stop it is 'parent.transform.childCount < limit'. You can improve this, both the timer mechanic and the condition used. The problem with this condition is working with something that may be used by another script (counting the childs of the objectToSpawn parent node). In other words, an element that it's outside your script's scope.  

That's two things actually.

- GetModifier(). You have set a single range to obtain values from (and in a hardcoded way). This will limit you to a square, in best of cases. And later, you apply a second random generation that will give you a negative value most of the time. So positions will concentrate mostly on one corner of that square.

That's two things again...

Now a little theory. For a rectangle, in your case, the fastest way to define it is by placing two position vectors: (x1, y1) and (x2, y2). If you want to take a new 'X' contained inside the rectangle, take a random value between x1 and x2. The same way with the new 'Y' value. 

Vector2 newPosition = new Vector2(Random.Range(x1,x2), Random.Range(y1,y2));

I imagine you want to try your own solution (I've noticed you're not reusing the code I left for you), and that's a good thing. Trying to find an alternative and possibly better solution. But remember that you will find later yourself with the problem of overlaping positions. What I think it is the original problem you write the post about.

Hope this helps you to find a solution. 

If you need to ask again, do not hesitate.

Cheers!

(1 edit)

Okay, I'll give test, and thank you.