Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by swifteria · Apr 01, 2015 at 04:41 PM · colliderboundaryinsantiate

Insantiating randomly within a collider.

Hi Unity community!

What I'm trying to find out is this; how do I insantiate trees within a collider/boundary. Just one or two, every 10 seconds or so. Here is a picture.

alt text

The island itself is not a terrain but rather a plane. I have, however, set up an invisible terrain on top, the exact same size (although I cannot think of a reason to use this).

TLDR; how do I insantiate a prefab every 10 seconds within a collider GameObject or some boundary of some sort.

Thanks!

untitled.png (23.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DoTA_KAMIKADzE · Apr 01, 2015 at 05:03 PM

Here is rough example, it should be enough for understanding, but be sure that you have only 1 instance of this script in whole gameworld:

 [SerializeField]
 private GameObject treeObj;
 private const float halfWidth = 300f;
 private const float halfHeight = 300f;
 private const int maxTreePerVawe = 2f;
 [SerializeField]
 private float waveWait = 10f;
 private void Start()
 {
     StartCoroutine(SpawnWaves());
 }
     private void SpawnTree(int count)
 {
     for (int i=0; i < count; i++)
     {
         // considering that Y:0 is the flat position of your plane
         Vector3 spawnPosition = new Vector3(UnityEngine.Random.Range(-halfWidth, halfWidth), 0f, UnityEngine.Random.Range(-halfHeight, halfHeight)); // you can get those values from "plane's "width/height"" if you want instead of adding them in script
         spawnPosition += transform.position; // where transform.position is basically the dot in the world which represents the center of your "plane", can be removed if it is 0:0:0, or changed if script is not attached to "plane"
         GameObject newTree = Instantiate(treeObj, spawnPosition, Quaternion.identity) as GameObject;
         // do here with newTree anything else that you want to do on initiation, like customizing Randomly size for example
     }
 }
 IEnumerator SpawnWaves()
 {
     yield return new WaitForSeconds(waveWait);//initial wait
     while (playerModel.isAlive)
     {
         SpawnTree(UnityEngine.Random.Range(1, maxTreePerVawe));
         yield return new WaitForSeconds(waveWait);//continue loop wait while player is alive or some other condition
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image swifteria · Apr 01, 2015 at 08:09 PM 0
Share

Thanks for the script, but it's not really what I am looking for. Although if I did some heavy tweaks to it, I could get it to work with my project. I was looking more for a sort of insantiate-within-a- collider kind of script if Unity allows that kind of thing. I really appreciate it though!

avatar image DoTA_KAMIKADzE · Apr 01, 2015 at 11:25 PM 0
Share

As I have commented in script you can set those values to whichever you need, e.g. fetch values from your collider like this:

    BoxCollider coll = GetComponent<BoxCollider>();
    Vector3 collSize = coll.size;
    Vector3 collCenter = coll.center;

And adjust randomization of spawn position accordingly, to fit your collider sizes.

P.S. Just remove halfWidth, halfHeight replacing those values with $$anonymous$$/max allowed values that you'll get from your collider - that simple.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Vault Over Collider Based Mesh 3 Answers

Using a collider to define the boundary of a level? 3 Answers

My object is going through boundaries Problem! 1 Answer

How to set a boundary around an object? 1 Answer

Internal collisions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges