- Home /
How would I properly put my script on multiple trees?
So I have programmed a script so that when the player is within a certain distance of the tree, they can gather wood from it. My only issue is that I want to include this for every tree. Is there an easy way to do this than placing every tree individually on the map?
Answer by getyour411 · Jan 30, 2014 at 11:08 PM
I'm going to suggest that the answer is No (short of procedural generation), which no offense to the OP, does not sound like an option he is ready to tackle just yet. OP originally said he doesn't want to place them one by one which is what, in the end, still happens with prefabs. I suspect he's asking if he can add a script to a tree, use that tree prefab in the Terrain Tree PAINTER and paint the trees - there answer is no, that will not work. Scripts attached to trees that are painted via Terrain tree painter do not work.
Thanks for the answer. Now we're getting somewhere. What do you suggest is the best way to get the trees on the terrain without placing them one by one?
Since the painter is not an option, really the only way is via script/procedure generated placement but that has issues too.
You probaby don't want trees scattered willynilly on your terrain, so how do you control where? There'll have to be zones or coords or some control structure.
One other lesson learned when I went down this road, is that mixing hand-placed vs painted trees can yield some funky visual results. Hard to explain but if you paint some trees then place some trees (either via hand or script) and Play & approach the tree area where they are mixed, you'll see how some trees seem to jump in front of others. At least that's what I saw when I last did this.
Unfortunatey I don't have a better answer because I put this issue on hold for my gamedev to work on a few other parts and haven't gotten back to it.
Its gonna be on a 4000x4000 terrain square and its also a survival game so I want the player to be able to chop down any tree they come across
This will be very tricky to interact with trees that are painted on the terrain, but it is possible.
On finding a tree and hitting its collider, the collider returned would be the terrain collider.
You would have to read the terrain data to find the tree you are colliding with, and then remove it from the terrainData.treeInstances list.
You can see an example of reading and modifying treeInstances data in my answer here : Remove Underwater Trees
Another way could be to paint trees without colliders, then use ideas from this answer to generate a collider at the tree being chopped position and work with that.
Recently stumbled upon this, think it's what you're after :
Part 22: Timed Trees : http://www.youtube.com/watch?v=h9I7cyYnrP$$anonymous$$
Part 25: Chopping Trees : http://www.youtube.com/watch?v=$$anonymous$$otIJ$$anonymous$$YQ7Dw
Answer by HobbyKid · Jan 31, 2014 at 05:02 AM
I have your answer. Got a bit excited cause as noob as I am, this question I can actually answer.
To do this go into your project folder and take the tree you want the player gather from and drag it into your scene. Then attach the script to the tree in your scene. This is so you have the collider style script your referring to on that inparticular game object. Next save that tree as a seperate gameobject; save it to your asset folder.
Finally to place this tree with the collider go to heirarchy--> terrain--> add trees. Be sure to add the tree that you just put the script on . Then you can mass place trees easily.
I wish that worked, but unfortunately its the scenario we discussed above, trees from the Terrain tree paint do not work with the scripts you assigned to the tree prefab before painting.
Your answer
Follow this Question
Related Questions
how to change object 0 Answers
Object won't collide with Player unless Player is moving 1 Answer
Distance from tagged objects problem 1 Answer
Changing player object UNET 1 Answer