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 KonstantinMaximus · Feb 08, 2015 at 05:46 PM · terraintree

Programatically placed trees end up in a wrong place

I am working on a terrain generator, I got my terrain going, so I decided to play around with trees.

I add 5 tree prototypes to the terrain data like so:

         List< TreePrototype > treePrototypes = new List< TreePrototype >();
         foreach ( GameObject treePrefab in this.treePrefabs )
         {
             TreePrototype treePrototype = new TreePrototype();
             treePrototype.prefab = treePrefab;
             
             treePrototypes.Add ( treePrototype );
         }
         terrainData.treePrototypes = treePrototypes.ToArray();

Then, I have an array of points that are within the terrain and I would like to place trees in those locations. I try to do so using:

         foreach ( Vector3 point in spawnPoints )
         {
             Vector3 onTerrainPosition = point;
             RaycastHit hit;
             if ( Physics.Raycast( point, -Vector3.up, out hit ) )
             {
                 if ( hit.transform.name == "Terrain" )
                 {
                     onTerrainPosition = hit.point;
                 }
             }
 
             TreeInstance newTree = new TreeInstance();
             newTree.position = onTerrainPosition;
             newTree.widthScale = 1;
             newTree.heightScale = 1;
             newTree.color = Color.yellow;
             newTree.lightmapColor = Color.yellow;
             newTree.prototypeIndex = Random.Range ( 0, terrainData.treePrototypes.Length );
             Terrain.activeTerrain.AddTreeInstance( newTree );
         }

All this works, except that all the trees are at "Tree Instance at (1.0, 0.0, 1.0)".

I thought maybe it's because terrain hasn't been placed in the scene yet, but I delayed the tree placement call and get the same result.

If during run time I click on "Mass Place Trees" everything works - I get tons of trees all over the place.

What could cause the positions to be off/reset/wrong? Maybe those locations don't have enough space? Any thoughts?

Thanks!

Comment
Add comment · Show 6
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 KonstantinMaximus · Feb 08, 2015 at 07:31 PM 0
Share

I also tried creating a completely flat terrain to see if terrain deformations causes collisions problems with trees... nope, trees are still at (1,0,1)

avatar image daneislazy · Feb 08, 2015 at 07:46 PM 0
Share

strange that it works fine after the level has loaded.

What controls the vector3 of spawnPoints? is that getting made too late, or if you are using a Random.Range(1, someNumber) for the x & z values is the someNumber getting defined too late?

Also, it probably wont change it, but you could add a large vertical vector to the point in the raycast so that you can still hit higher terrain like Physics.Raycast(point + Vector3.up * 50f, -Vector3.up, out hit)

avatar image meat5000 ♦ · Feb 08, 2015 at 07:58 PM 0
Share

Debug.Log everything. You can't really see what's happening otherwise.

avatar image KonstantinMaximus · Feb 08, 2015 at 08:06 PM 0
Share

I generate a texture for high map, those spawnPoints are sampled from it. I use this code to debug those points and they seem to be right:

         Gizmos.color = Color.yellow;
         if ( spawnPoints != null )
         {
             foreach ( Vector3 spawnPoint in spawnPoints )
             {
                 Gizmos.DrawSphere ( spawnPoint, 1.0f );
             }
         }

Unless of course terrain space is not the same as the world space...

avatar image daneislazy · Feb 08, 2015 at 08:33 PM 0
Share

hmm well I would think that the Gizmos would use world space since you didn't call it with the terrain object.

so if the tree generation works after the scene has been created can you do something like Invoke("$$anonymous$$akeTrees", 2f); in the start. just to make sure everything is in place already.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KonstantinMaximus · Feb 09, 2015 at 12:20 AM

So, I figured it out. Tree positions have to be in 0 to 1 space. So, I just did this:

 newTree.position = new Vector3 ( point.x / this.mapWidth, 0, point.z / this.mapDepth );
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why are the trees dark when you put them to the scene and zoom in? 1 Answer

Tree Creator Problems 1 Answer

Can't use tree brush tool 0 Answers

Tree brush making very tiny trees 0 Answers

Initial rotation of trees with a script 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