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
1
Question by CaptainChristian · Jul 02, 2013 at 11:55 AM · terrainobjectsnap

Snap Terrain to Building/Structure

Hi,

I would like my terrain to snap to buildings placed on it. I got the basics, but I cannot get it to work correctly. Sometimes the results go into the right direction, however often they are far off.

In a nutshell I use the transform position as a reference height and then interpolate between the lower vectors of the bounding volume. The code for terraforming itself can be found on the forums already, but I will post it to show where I am standing currently.

         float distance = Vector3.Distance (start, end);
         float segmentSize = 0.2f;
         int tiles = Mathf.CeilToInt (distance / segmentSize);
         
         for (int t = 0; t < tiles; t++) {
             
             Vector3 tempPosition = Vector3.Lerp (start, end, t / (tiles-1));
         
             Terrain tempTerrain = GetTerrainTile(tempPosition);
             // get the normalized position of this game object relative to the terrain
             Vector3 tempCoord = (tempPosition - tempTerrain.gameObject.transform.position);
             Vector3 tempCoordNormalized = new Vector3(0,0,0);
             tempCoordNormalized.x = tempCoord.x / tempTerrain.terrainData.size.x;
             tempCoordNormalized.y = tempCoord.y / tempTerrain.terrainData.size.y;
             tempCoordNormalized.z = tempCoord.z / tempTerrain.terrainData.size.z;
              
             // get the position of the terrain heightmap where this game object is
             posXInTerrain = (int)(tempCoordNormalized.x * tempTerrain.terrainData.heightmapWidth);
             posYInTerrain = (int)(tempCoordNormalized.z * tempTerrain.terrainData.heightmapWidth);
             
             // we set an offset so that all the raising terrain is under this game object
             int size = 1;
             float desiredHeight = height/tempTerrain.terrainData.heightmapScale.y;
              
             // get the heights of the terrain under this game object
             float[,] heights = tempTerrain.terrainData.GetHeights (posXInTerrain, posYInTerrain, size, size);
             // we set each sample of the terrain in the size to the desired height
             for (int i=0; i<size; i++)
                 for (int j=0; j<size; j++)
                     heights [i, j] = desiredHeight;
             
             // set the new height
             tempTerrain.terrainData.SetHeights (posXInTerrain, posYInTerrain, heights);
         }

Any help or hint would be appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

Object falls through terrain 1 Answer

Raycast Snapping Problem. 0 Answers

Object falling through terrain with script 2 Answers

Export Whole Scene to 3ds Max 2 Answers

How to convert mesh object to terrain Unity 4.5 0 Answers


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