Search Unity

A bug with my terrain generator

Discussion in 'Scripting' started by raider13209, Aug 23, 2021.

  1. raider13209

    raider13209

    Joined:
    Aug 19, 2020
    Posts:
    5
    My terrain generator isn't working, like I'm sure I'm just being a dumb person here but im not exactly sure what went wrong in my script. this is the part i suspect isn't working. if anyone could help i would really appreciate it.(its a low poly generator)


    Code (CSharp):
    1.  
    2. vertices[0 + verts] = new Vector3(x + 0, Mathf.PerlinNoise(x + 0, z + 0) * 5, z + 0);
    3.                 vertices[1 + verts] = new Vector3(x + 0, Mathf.PerlinNoise(x + 0, z + 1) * 5, z + 1);
    4.                 vertices[2 + verts] = new Vector3(x + 1, Mathf.PerlinNoise(x + 1, z + 0) * 5, z + 0);
    5.                 vertices[3 + verts] = new Vector3(x + 1, Mathf.PerlinNoise(x + 1, z + 1) * 5, z + 1);
    6.                 vertices[5 + verts] = new Vector3(x + 0, Mathf.PerlinNoise(x + 0, z + 1) * 5, z + 1);
    7.                 vertices[4 + verts] = new Vector3(x + 1, Mathf.PerlinNoise(x + 1, z + 0) * 5, z + 0);
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Neither are we, since you haven't even given us the slightest clue as to why you think it isn't working!

    Remember: nobody here can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Beyond that, this process can help you learn about your code and how it is working:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. raider13209

    raider13209

    Joined:
    Aug 19, 2020
    Posts:
    5
    I'm sorry about the unclarity in my main post and all, it was my first time seeking assistance on these forums. The problem is that the noise isn't functioning properly as no height data is being transferred onto the vertices that script is meant to be generating. I guess I was just a bit unclear in the prior post, and I'm sorry for that.
    I know that the vertices and script are generating properly and its on the right object as the map is generating but as i said earlier no data is being transferred onto the vertices y coordinate as it i meant to by the noise function.
    Also just in case I'm using unity 2020.3.7f1.

    Edit: It seems that all of the vertices are rising to a certain level, I'm not sure what is specifically causing this, but I believe it is the noise function.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    When you used Debug.Log() to print what's coming out of the noise function, what did you learn?
     
    exiguous likes this.
  5. raider13209

    raider13209

    Joined:
    Aug 19, 2020
    Posts:
    5
    ok.... they seem to be returning the same value for each one
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Don't stop now, you're on a roll!

    Print out what you're passing into it.

    Copy out that line of noise function, put it in a fresh script, feed it fresh numbers and figure out what's going on!

    Check the docs for the noise function. Are you misunderstanding how it works?

    This is just debugging 101... dive in, figure out what's happening.
     
    exiguous likes this.
  7. raider13209

    raider13209

    Joined:
    Aug 19, 2020
    Posts:
    5
    Ok after looking at some other people with the exact same problem I've worked out how to fix it thx for the help, its really appreciated!
    There is now another problem with the vertices :( i'll work it out myself though
    its a problem in the main generator itself but the stuff i learnt really helped
     
    Last edited: Aug 23, 2021
    Kurt-Dekker likes this.