- Home /
Chopping down trees painted onto the terrain
Hello. So, I'm making a strategy game where the player will be able to place buildings and for the workers who will build the particular building to be able to cut down the trees that are in the way. So, how do I go about removing trees from the terrain within a certain radius?
Thanks in advance.
Answer by Arcana96 · Jun 10, 2017 at 01:14 AM
I managed to come up with a fairly hacky solution but it's working.
What I done was I have two terrains that have the same topography. However, one has painted trees and the other does not. I convert all of the painted trees into game objects and then remove the terrain with the painted trees and activate the one without. After that you'll essentially have the same terrain but all the trees are now game objects.
Here's the solution:
public void Convert() {
TerrainData data = terrain.terrainData;
replacements = new TreeInstance[data.treeInstances.Length];
for (int i = 0; i < replacements.Length; i++) {
replacements[i] = new TreeInstance();
}
float width = data.size.x;
float height = data.size.z;
float y = data.size.y;
foreach (TreeInstance tree in data.treeInstances) {
Vector3 position = new Vector3(tree.position.x * width, tree.position.y * y, tree.position.z * height);
GameObject _tree = Instantiate(trees[tree.prototypeIndex], position, Quaternion.identity);
_tree.transform.SetParent(newTerrain.transform);
}
terrain.gameObject.SetActive(false);
newTerrain.gameObject.SetActive(true);
Answer by jmgek · Jun 09, 2017 at 11:46 PM
Use OverLapSphere: https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
public class ExampleClass : MonoBehaviour
{
void RemoveTrees(Vector3 center, float radius)
{
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
int i = 0;
while (i < hitColliders.Length)
{
hitColliders[i].SendMessage("Remove tree");
i++;
}
}
}
I know how to do that. The part about removing trees from the terrain is the problem.
http://answers.unity3d.com/questions/650308/how-do-i-interact-with-terrain-trees.html
First thing that google gave me.
I also googled it didn't work in my case. Almost everything I tried hasn't.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Why is my terrain lowering when editted at runtime to raise? 0 Answers
Rigidbody.velocity giving weird result 0 Answers