• 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 beau101023 · May 21, 2015 at 06:17 AM · c#terraingenerationgeneratorflat

Terrain Generator Producing Completely Flat Results.

My terrain generator is failing to produce any variation in height whatsoever. The logic seems to work out fine on paper. The system is supposed to work by alternating between a system of deriving rows of cube heights progressively, taking the height of the previous cube and adjusting the next up, down, or not at all, and a system of deriving the next row's height map from the previous row.

 using UnityEngine;
 using System.Collections;
 
 
 public class Terrain_Generator : MonoBehaviour 
 {
     public GameObject Target_Obj;
     int width = 5;
     int iteration;
     int length = 5;
     public int height;
     int prevPos;
     int height0;
     int height1;
     int height2;
     int height3;
     int height4;
     int height5;
     int horizPos;
     void Start () 
     {
         //j controls the x coord.
         for (int j=0; j<width; j = j + 1) 
         {
             prevPos = 0;
             //i controls the z coord.
             for (int i=0; i<length; i = i + 1) 
             {
                 /*Sets the horizPos to generate cubes 1 unit below or above the previous cube's 
                 position, as well as in the same position height-wise.*/
                 if(j == 0 && j == 2 && j == 4)
                 {
                     horizPos = Random.Range (prevPos + height, prevPos - height * 2);
                 }
                 //Generates the cube's height based on the corresponding cube in the previous row.
                 if(j == 1 && j == 3 && j == 5)
                 {
                     if(i == 0) 
                     {
                         horizPos = Random.Range (height0 + height, height0 - height * 2);
 
                     }
                     if(i == 1) 
                     {
                         horizPos = Random.Range (height1 + height, height1 - height * 2);
                     }
                     if(i == 2) 
                     {
                         horizPos = Random.Range (height2 + height, height2 - height * 2);
                     }
                     if(i == 3) 
                     {
                         horizPos = Random.Range (height3 + height, height3 - height * 2);
                     }
                     if(i == 4) 
                     {
                         horizPos = Random.Range (height4 + height, height4 - height * 2);
                     }
                     if(i == 5) 
                     {
                         horizPos = Random.Range (height5 + height, height5 - height * 2);
                     }
                 }
                 Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
                 prevPos = horizPos;
                 //Stores y coord of previous j iteration's cubes in order.
                 if(i == 0)
                 {
                     height0 = horizPos;
                 }
                 if(i == 1)
                 {
                     height1 = horizPos;
                 }
                 if(i == 2)
                 {
                     height2 = horizPos;
                 }
                 if(i == 3)
                 {
                     height3 = horizPos;
                 }
                 if(i == 4)
                 {
                     height4 = horizPos;
                 }
                 if(i == 5)
                 {
                     height5 = horizPos;
                 }
             }
         }
     }
     void Update () 
     {
 
     }
 }
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
1
Best Answer

Answer by tanoshimi · May 21, 2015 at 06:54 AM

The parameters for Random Random.Range are (min, max).

It looks like you're supplying (max, min) every time.

Comment
Add comment · Show 1 · 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 beau101023 · May 21, 2015 at 07:49 PM 0
Share

D'oh. Thanks for pointing this out. I feel silly now. :P

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Code Loops Indefinitely 1 Answer

[C#] 2D infinite Terrain Generation with prefabs?! -> lag 0 Answers

Procedural 2D Tilemap Terrain - Mesh or Unity Tilemaps? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges