• 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 tatsu006 · Mar 30, 2018 at 07:24 PM · aitreetrees

Tree spawning problem

i want to make a tree script that spawns new trees around old trees, but i keep having the issue of them spawning in large numbers above the value i have set for them wich is 10, also my trees spawn at 0,0,0, on the map wich is not what i need. how can i spawn them next to an other tree whitin a certain distance ofcourse. any advice would be appreciated. thx

 public float MaxTreeHeight = 10.0f;                //Tree Growth Max Hieght
 public float TreeGrowthSpeed = 0.1f;            //Tree Growth Speed
 public bool TreeGrowthEnabled = true;            //Growing Tree?
 public bool Spawning = true;                    //Spawn Tree?
 public float SpawningTimer = 0.1f;                //If Spawn = True, Set Timer To Instantiate Tree
 public GameObject MyTreeTypePrefab;                //Assign Witch Tree Type Should be Spawnd
 public float MyTreeTypeDistance = 4.0f;            //Spawn Tree Distance from main Tree
 public int i = 0;                                //Variable for Tree Spawning Distance
 public int SpawnMaxNumber = 0;                    //Variable for max number of Tree Spawns 




 void start () {

 }

 void Update () 
 //Growing Tree?
 {
     if (transform.localScale.x < MaxTreeHeight)                     //max Tree hieght
     {
         TreeGrowthEnabled = true;
         transform.localScale += new Vector3(0.1f,0.1f,0.1f) * TreeGrowthSpeed * Time.deltaTime;    //growth speed
     }
     else if (transform.localScale.x > MaxTreeHeight)
     {
         TreeGrowthEnabled = false;
         Spawn ();
     }
 }

 void Spawn ()
 //Spawn Tree?
 {
     if (transform.localScale.x > 2 && SpawnMaxNumber < 10)
         for (i = 0; i < 9; i++) {
             Spawning = true;
             Instantiate (MyTreeTypePrefab, new Vector3 (i * 2.0f, 0, 0), Quaternion.identity);
             SpawnMaxNumber += 1;
             Debug.Log ("Spawned A Tree" + i);
         }
     if (transform.localScale.x < 1)
     {
         Spawning = false;
     }
 }

}

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
0

Answer by Rygaran · Mar 30, 2018 at 08:41 PM

Insisde of spawn() you check for spawMaxNumber and then loop 8 timesin the for, this will make it so that you will spawn 16 times. On instantiate, you're passing "new Vector3 (i * 2.0f, 0, 0)" as the spawn position, this is in world coordinates, you want to do "transform.position + positionAwayFromTree", how you calculate positionAwayFromTree is up to you. There's many ways to calculate it: Random, going in a circle arround the tree, or other crazy stuff. You then also would probably want trees to dont spawn inside eachother, wich means, in the case of random, for example, you need to check for collision, so keep that in mind.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trees on slopes are floating 1 Answer

Painted trees disappear when the scene is saved. 2 Answers

how to add collider to trees made with terrain tool? 1 Answer

Messed up Trees in only my scene (not new scene) 1 Answer

Tree Brush not placing trees correctly 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