Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 07:07:12 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to put prefabs spawn into an shape area in Unity
Pages: [1]
Print
Author Topic: How to put prefabs spawn into an shape area in Unity  (Read 2080 times)
ArtInkStudios
Level 0
**


Art Ink Studio Logo


View Profile WWW
« on: October 09, 2021, 02:42:41 AM »

Hi, I forgot to mention this sooner that I'm new in coding and last time posted on asking for how to spawn position to which I successfully done. But, now need help in wanting the spawning prefabs located within a rectangle area in unity. Here are ideas of want I mean from these web links.  how to create a spawn point within shape area for unity https://www.google.com/search?q=how+to+create+a+spawn+point+within+shape+area+for+unity&tbm=isch&ved=2ahUKEwjis6XB27DzAhUzqHIEHbL2DKkQ2-cCegQIABAA&oq=how+to+create+a+spawn+point+within+shape+area+for+unity&gs_lcp=CgNpbWcQA1Ck58kCWOD1yQJg0v7JAmgAcAB4AIABowGIAfcEkgEDNC4ymAEAoAEBqgELZ3dzLXdpei1pbWfAAQE&sclient=img&ei=3-1aYaLIJbPQytMPsu2zyAo&bih=625&biw=1366&hl=en            
                                                                                                                         
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;

    }

}
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #1 on: October 26, 2021, 03:44:50 PM »

Any arbitrary shape? Or are there specific shapes you want to use.

Like are you ok with spawning within a plane/cube/sphere/cylinder or do you want to spawn within the volume of some path or 3d model?
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic