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 cwperkins83 · Oct 28, 2013 at 07:18 PM · terraingenerationtilesheightgenerate

Tile Terrain Generation

I'm trying to create a terrain out of a grid of individual 4-vertex planes which will eventually be customized for different materials(i.e. clay, dirt, sand, etc). I am able to create a grid of flat planes, but when I try to alter the height of individual vertices, only the bottom left square(Tile00) renders and the remaining are instantiated as empties.

Here is my script. I can't quite figure out what's wrong, and I'm not getting any errors other than two variables not being used.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnTester : MonoBehaviour {
     public GameObject board;
     public Transform tile_prefab_;
     public int board_size_x_;
     public int board_size_z_;
     public int nameX;
     public int nameZ;
     
     void Start(){
         GenerateTiles();
         GenerateHeight();
     }
     
     void GenerateTiles () {
           board = new GameObject();
           board.name = "Board";
         for ( int x = 0; x < board_size_x_; x++ ) {
              for ( int z = 0; z < board_size_z_; z++ ) {
                 Transform tile = (Transform)Instantiate(tile_prefab_,new Vector3(x * 10,0,z * 10),Quaternion.identity);
                 tile.name = "Tile" + x + z;
                 tile.parent = board.transform;
                
                 
             }
         }
         
     }
     
     void GenerateHeight () {
         for ( int x = 0; x < board_size_x_; x++ ) {
              for ( int z = 0; z < board_size_z_; z++ ) {
                 Transform tile = GameObject.Find("Tile" + x + z).transform;
                 
                 Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
                 Vector3[] vertices = mesh.vertices;
 
                 if(tile.name == "Tile00"){
                     for(int i = 0; i < vertices.Length; i++){
                         vertices[i] += Vector3.up * Random.Range(0, 11);
                     }
                     mesh.vertices = vertices;
                     mesh.RecalculateBounds();
                     mesh.RecalculateNormals();
                     MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
                     meshc.sharedMesh = mesh;
                 }
                 else{
                     
                     int tempX = x - 1;
                     int tempZ = z - 1;
                     if(tempX < 0)
                         tempX = 0;
                     if(tempZ < 0)
                         tempZ = 0;
                     
                     string leftName = "Tile" + tempX + z;
                     string botName = "Tile" + x + tempZ;
                     
                     Transform leftTile = GameObject.Find(leftName).transform;
                     Transform botTile = GameObject.Find(botName).transform;
                     
                     if(leftTile){
                         Vector3[] leftVerts = leftTile.GetComponent<MeshFilter>().mesh.vertices;
                         Vector3 leftVertTop = leftVerts[0];
                         Vector3 leftVertBot = leftVerts[3];
                         vertices[1] = leftVertTop;
                         vertices[2] = leftVertTop;
                     }
                     else{
                         vertices[1] += Vector3.up * Random.Range(0, 11);
                         vertices[2] += Vector3.up * Random.Range(0, 11);
                     }
                     
                     if(botTile){
                         Vector3[] botVerts = botTile.GetComponent<MeshFilter>().mesh.vertices;
                         Vector3 botVertLeft = botVerts[2];
                         Vector3 botVertRight = botVerts[0];
                         vertices[3] = botVertRight;
                     }
                     else{
                         vertices[3] += Vector3.up * Random.Range(0, 11);
                         
                     }
                     
                     vertices[0] += Vector3.up * Random.Range(0, 11);
                     mesh.vertices = vertices;
                     mesh.RecalculateBounds();
                     MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
                     meshc.sharedMesh = mesh;
                 }
             }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tylo · Oct 28, 2013 at 07:33 PM

I am not sure what it is, but there is some kind of bad logic inside of your else block starting on line 50.

I commented it out and processed all tiles as if they were treated as "Tile00" is, and the meshes were all unique (if crazy looking).

In other words, it is not a case of Unity duplicating anything internally (which can happen sometimes). All your meshes are instanced and unique.

Comment
Add comment · Show 3 · 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 cwperkins83 · Oct 28, 2013 at 08:37 PM 0
Share

Thanks for your response. I don't suppose you have any ideas? Also, can you kind of clarify your last line?

avatar image tylo · Oct 28, 2013 at 09:09 PM 0
Share

With things like shared$$anonymous$$aterials and shared$$anonymous$$eshes, sometimes Unity will alter every object that happens to use it. I know this is the case with shared$$anonymous$$aterials.

I'm afraid I don't have any advice on how to fix what you're trying to do in your else statement, as I am not sure what the final intent of your gameboard is supposed to look like.

All I can say for sure is, the coordinates you are giving your vertexes are incorrect, and it is causing the meshes to appear invisible. You'll have to do some serious debugging to find out what those value are being set to.

avatar image cwperkins83 · Oct 28, 2013 at 10:00 PM 0
Share

I figured that might be the case. Upon debugging the vertices, the Vector3's that I'm given are something like (1,0,-1). Is it possible that it's doing it locally in the plane from the center(origin) of the mesh? And if so, is there a way to get a global Vector3 and use that to assign the vertices.

$$anonymous$$y intent is to create a tiled terrain. If you're familiar with Wurm Online, a terrain like that. Where there is a random height to each corner and the adjacent tile will copy so that they match up perfectly.

Upon reading the Scripting Reference a bit more, would TransformPoint be what I need? And if so, how would I work it in?

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

16 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

Related Questions

Runtime mesh generation from text file 0 Answers

Generate Random Terrain on Start 1 Answer

Minecraft (Cube) Terrain 1 Answer

Join terrain tiles with different height... 1 Answer

Problem with heightmap and terrain height maxing out at 10000, proportions *Solved* 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