- Home /
Dealing with mass tree spawning
So I have a terrain mesh being generated through a series of scripts, it is infinite and works well for what it. The problem lies when adding trees, at first I tried making them spawn on only on a certain colour (green being grass) but to no success, next, I tried making them spawn then drawing a raycast down from each tree and moving them to that position but again to no success. Finally, I decided to spawn them above when the terrain would spawn and then using Ridgidbody.gravity to make them drop and freezing them at the position the hit which works, along side collision detection for if the collide with say a mountain or each other. This system works but as you would imagine have 1000+ trees spawning and falling for 1 seconds is quite laggy and takes about 5 seconds to balance out.
My question is could anyone think of a better way to do this.
Thanks
Answer by TheSOULDev · Aug 26, 2017 at 10:36 PM
Add a physics raycaster component to the ground, spawn a tree at a height you know there is no terrain, cast a ray from the bottom of the tree (I guess the root) and calculate the distance from the ground, transform.translating the tree over that distance. Don't load all trees at once but try loading around 10 or so trees per frame (work out what seems to work in a way it doesn't kill frames).
Always avoid the combination of physics + a lot of objects unless you'll have time to render that kind of simulation.
Your answer
Follow this Question
Related Questions
Adding gravity to my gameobject causes lagg 1 Answer
Why do cubes in unity overlap a little with the ground? 1 Answer
Apply gravity to enemy objects without Rigidbody (?) 1 Answer
rigidBody collision with wall stops vertical movement. 1 Answer
controller.Move doesn't stay grounded when walking down slope 3 Answers