Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CaRmAgE · Apr 02, 2018 at 09:16 PM · terrain2d gamecustom editorterraindataterraincollider

Possible to make custom editor script to set terrain height?

I'm working on a collab project for a 2D racing game involving hills. Since making smooth hills with an EdgeCollider2D would be a pain in the butt, I decided working with 3d colliders would be the best solution (plus I've experienced weird physics with 2d colliders in the past).

Creating the hills with a terrain collider wasn't bad at first. I used a view from behind the track (z-axis) to "paint in" the sprite using the terrain collider:

Back view of terrain (Side note: These hills are just for testing the racing mechanics and not an actual track.)

Seemed okay at first, until I viewed everything from the side: Side view of terrain

Oops. I wasn't expect all the hills to be unaligned. A solution I'd like to use is to make the height the same along the entire z-axis. I could do that with a script by looping over the height matrix, looping over the z-axis twice: the first time getting the max height, and setting the height the second time around. Of course, doing that at runtime would be completely unoptimal. It would be nice to have a button on a component that does this in the editor, but I'm not familiar with making custom editor scripts. Is it possible to do this with the terrain? Is there a better way of solving my problem?

(If necessary, I'll make the custom editor script myself, but I need to know how to make one.)

Comment
Add comment · Show 2
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 mgear · Apr 03, 2018 at 06:19 AM 0
Share

if you are making it as fully 2D, then terrain would be quite heavy for that purpose.. could try with flat mesh ins$$anonymous$$d. but for terrain can set heights using https://docs.unity3d.com/ScriptReference/TerrainData.SetHeights.html and to make editor script https://docs.unity3d.com/ScriptReference/EditorWindow.html

avatar image CaRmAgE · Apr 03, 2018 at 10:24 PM 0
Share

Well, we wanted to add trees later as background decorations (plus I don't really have any external programs for making meshes).

I didn't know we could make our own windows. I'll see if I can figure out the documentation and come up with something.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CaRmAgE · Apr 04, 2018 at 09:20 PM

I figured it out. This is the code I created to solve my problem (in case anyone else finds it useful for their 2D projects):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 
 public class HillsGenerator2DWindow : EditorWindow {
 
     public GameObject hills = null;
     Terrain terrain;
 
     // Use this for initialization
     [MenuItem("Window/Generate Hills...")]
     static void Init(){
         EditorWindow window = EditorWindow.GetWindow(typeof(HillsGenerator2DWindow));
         window.titleContent.text = "2D Hills Creator";
         window.position = new Rect(0,0,180,80);
         window.Show();
     }
 
     // OnGUI is called when drawing the window
     void OnGUI(){
         hills = (GameObject)EditorGUI.ObjectField(new Rect(3,3,position.width - 6,20),
             hills,typeof(GameObject),true);
         if(hills){
             terrain = hills.GetComponent<Terrain>();
             if(!terrain){
                 EditorGUI.LabelField(new Rect(3,25,position.width - 6,20),
                     hills.name+" does not have a Terrain component!");
             } else if(!terrain.isActiveAndEnabled){
                 EditorGUI.LabelField(new Rect(3,25,position.width - 6,20),
                     hills.name+"'s Terrain component must be enabled!");
             } else if(GUI.Button(new Rect(3,25,position.width - 6,20),"Generate Hills") &&
                 EditorUtility.DisplayDialog("Confirmation",
                     "Are you sure you want to '2D-ize' the TerrainData on "+hills.name+
                     "? You cannot undo this action.","Start Operation","Cancel")){
                 EditorUtility.DisplayProgressBar("Generating Hills...","Initializing...",0f);
                 float heightmapHeight = (float)terrain.terrainData.heightmapHeight;
                 foreach(int i in GenerateHills(terrain)){
                     float progress = i / heightmapHeight;
                     if (progress < 1.0f){
                         EditorUtility.DisplayProgressBar("Generating Hills...",
                             "Updating samples "+(i+1)+" / "+heightmapHeight+"...",progress);
                     } else {
                         EditorUtility.DisplayProgressBar("Generating Hills...","Finalizing...",
                             1.0f);
                     }
                 }
                 EditorUtility.ClearProgressBar();
                 EditorUtility.DisplayDialog("Hills Generated","The terrain on "+
                     hills.name+" has been '2D-ized'.","OK");
             }
         } else {
             EditorGUI.LabelField(new Rect(3,25,position.width - 6,20),"Select a GameObject.");
         }
     }
 
     // OnInspectorUpdate is called whenever a change is made in the inspector view
     void OnInspectorUpdate(){
         Repaint();
     }
 
     // Update the terrain heightmap to make heights the same across the entire Z-axis
     private IEnumerable<int> GenerateHills(Terrain terrain){
         TerrainData terrainData = terrain.terrainData;
         float maxHeight, currentHeight;
         int heightmapWidth = terrainData.heightmapWidth;
         int heightmapHeight = terrainData.heightmapHeight;
         float[,] heightmap = terrainData.GetHeights(0,0,heightmapWidth,heightmapHeight);
         for(int x=0; x<heightmapHeight; x++){
             maxHeight = 0f;
             yield return x;
             for(int z=0; z<heightmapWidth; z++){
                 currentHeight = heightmap[z,x];
                 if(currentHeight > maxHeight){
                     maxHeight = currentHeight;
                 }
             }
             for(int z=0; z<heightmapWidth; z++){
                 heightmap[z,x] = maxHeight;
             }
         }
         yield return heightmapHeight;
         terrainData.SetHeights(0,0,heightmap);
     }
 }
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

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

Related Questions

Object2Terrain Creates a Invalid Collider. Help please. 1 Answer

How can i get terrain data from Terrain.asset? 0 Answers

Unity tree colliders acting very strange 0 Answers

[SOLVED] Unity 5 - Runtime Terrain Deformation Collider Bug 4 Answers

terrainData.heightmapTexture float value range 2 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