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 fred_gds · Dec 26, 2013 at 09:19 PM · editorterraingame object

flatten Terrrain under object

Hey

I need to flatten my terrain under the the objects I imported (roads & buildings) so that it's flat around it. The problem is that the roads aren't flat so I kind of wanted to ray cast ups/down in order to get the right terrain hight at those points. But I am also unable to code it myself.

It would be great if somebody could help me out with some code are asset!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nastasache · Dec 27, 2013 at 03:56 PM

This is just a very quick and not optimized script to give you an idea. Place terrain at coordinates 0,0,0; place a FPS Controller on it, attach script to FPS Controller and walk a while. Then press H. Of course, you have to put some more logic to create roads on this way but this is an example how you can manipulate terrains data.

Flatter.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Flatter : MonoBehaviour {
     
     TerrainData terrData;
         
     float[,] heightmapData; // prepare a matrix with terrain points
     float ratio; // ratio between player position and terrain points
     
     // Use this for initialization
     void Start () {
         
         terrData = Terrain.activeTerrain.terrainData;
         int terrRes = terrData.heightmapResolution;
         Vector3 terrSize = terrData.size;
         
         heightmapData = terrData.GetHeights(0, 0, terrRes, terrRes); // heights we will change during of walking
         
         ratio = terrSize.x / terrRes;
         
         Debug.Log ("heightmapData="+heightmapData +" terrRes="+terrRes+" terrSize="+terrSize + " ratio="+ratio);
     }
     
     // Update is called once per frame
     void Update () {
 
         int terrainPointX = Mathf.CeilToInt(transform.position.x / ratio);
         int terrainPointZ = Mathf.CeilToInt(transform.position.z / ratio);
         
         Debug.Log (" terrainPointX="+terrainPointX+" x terrainPointZ="+terrainPointZ +": set height to 0.0");
         
         heightmapData[terrainPointZ,terrainPointX ] = 0.0f; // move terrain point to 0 (example)
 
     }
             
     
     void OnGUI() {
         if (Input.GetKey(KeyCode.H)){
             Debug.Log("Saving");
 
             terrData.SetHeights(0, 0, heightmapData); // save terrain heights back
         }
     }
 }
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
avatar image
0
Wiki

Answer by fred_gds · Dec 27, 2013 at 04:12 PM

Thanks. That's just what I needed. I guess that if I kind of throw a grid over ever the entire scene and ray cast vertical collisions, that then I should be able to adjust the entire terrain by that.

Could somebody please make this comment? Thanks ;)

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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make CustomEditor as in Terrain script ? 1 Answer

Need help with Hollow Terrain 1 Answer

Terrain grass invisible in editor 1 Answer

Tree creator and terrain editor 0 Answers

Scripts on tree prefab don't respond? 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