Search Unity

Question Instantiated cube prefab different Y value than vertex created as same location with same Y value?

Discussion in 'Scripting' started by wardhk, Apr 21, 2023.

Thread Status:
Not open for further replies.
  1. wardhk

    wardhk

    Joined:
    Aug 7, 2021
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Ex = System.Exception;
    5.  
    6. public class TerrainGen : MonoBehaviour
    7. {
    8.     public int width = 16;
    9.     public int height = 50;
    10.  
    11.     public GameObject myPrefab;
    12.  
    13.     private MeshFilter meshFilter;
    14.     private static float noiseScale = .05f;
    15.  
    16.     Mesh mesh;
    17.     Vector3[] vertices;
    18.     int[] triangles;
    19.  
    20.     void Start()
    21.     {
    22.  
    23.         GeneratePerlinNoiseMesh(0, 0);
    24.     }
    25.  
    26.     public void GeneratePerlinNoiseMesh(int offsetX, int offsetZ) {
    27.         mesh = new Mesh();
    28.         GetComponent<MeshFilter>().mesh = mesh;
    29.         vertices = new Vector3[(width+1) * (width+1)];
    30.         for (int i = 0, x = 0; x <= width; x++) {
    31.             for (int y = 0; y <= height; y++) {
    32.                 for (int z = 0; z <= width; z++) {
    33.                     if (getBlock(x + offsetX * width, y, z + offsetZ * width) && (!isSurrounded(x + offsetX * width, y, z + offsetZ * width))) {
    34.                         Debug.Log(y);
    35.                         GameObject cube = Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);
    36.                  
    37.                         Debug.Log(i + "" +  new Vector3(x, y, (i%(width + 1))));
    38.                         vertices[i] = new Vector3(x, y, (i%(width + 1)));
    39.                         i++;
    40.                     }
    41.                 }
    42.             }
    43.  
    44.         }
    45.         triangles = new int[width * width* 6];
    46.  
    47.         int vert = 0;
    48.         int tris = 0;
    49.  
    50.         for (int x = 0; x < width; x++) {
    51.             for (int z = 0; z < width; z++) {
    52.                 triangles[tris + 0] = vert + 0;
    53.                 triangles[tris + 2] = vert + width + 1;
    54.                 triangles[tris + 1] = vert + 1;
    55.                 triangles[tris + 3] = vert + 1;
    56.                 triangles[tris + 5] = vert + width + 1;
    57.                 triangles[tris + 4] = vert + width + 2;
    58.  
    59.                 vert++;
    60.                 tris += 6;
    61.             }
    62.             vert++;
    63.         }
    64.         UpdateMesh();
    65.     }
    66.  
    67.     void UpdateMesh() {
    68.  
    69.         mesh.Clear();
    70.  
    71.         mesh.vertices = vertices;
    72.         mesh.triangles = triangles;
    73.  
    74.         mesh.RecalculateNormals();
    75.     }
    76.  
    77.     public static bool getBlock(int x, int t, int z) {
    78.         float density = ((Perlin3D(x * noiseScale, t * noiseScale, z * noiseScale)*2)-1);
    79.         float yAverage = (t*2) / 100.0f;
    80.         yAverage = (yAverage * 2) - 1;
    81.  
    82.         //Increase yAverage divison for more moutains, decrease for flatter
    83.         density = (density + (yAverage/1.2f)) / 2;
    84.         if (density >= 0) {
    85.             return true;
    86.         } else {
    87.             return false;
    88.         }
    89.  
    90.     }
    91.     public static float Perlin3D(float x, float t, float z) {
    92.         float ab = Mathf.PerlinNoise(x, t);
    93.         float bc = Mathf.PerlinNoise(t, z);
    94.         float ac = Mathf.PerlinNoise(x, z);
    95.  
    96.         float ba = Mathf.PerlinNoise(t, x);
    97.         float cb = Mathf.PerlinNoise(z, t);
    98.         float ca = Mathf.PerlinNoise(z, x);
    99.  
    100.         float abc = ab + bc + ac + ba + cb + ca;
    101.         return abc / 6f;
    102.     }
    103.  
    104.     bool isSurrounded(int x, int t, int z) {
    105.         int[] dx = {-1, 1, 0, 0, 0, 0};
    106.         int[] dy = {0, 0, -1, 1, 0, 0};
    107.         int[] dz = {0, 0, 0, 0, -1, 1};
    108.  
    109.         for (int i = 0; i < 6; i++) {
    110.             if (!getBlock(x + dx[i], t + dy[i], z + dz[i])) {
    111.                 return false;
    112.             }
    113.         }
    114.  
    115.         return true;
    116.     }
    117.  
    118. }

    In my GeneratePerlinNoiseMesh function I instantiate the cube object using
    Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);. After I create a vertex at the same location as the cube using
    vertices = new Vector3(x, y, (i%(width + 1)));. If you look at the image Debug.Log(i + "" + new Vector3(x, y, (i%(width + 1)))); outputs y value is 22 for vertex number 18 and Debug.Log(y); is also outputting 22 yet the position y for the cube instantiated at the same X and Z position is 23 using the same y variable as the vertex? Both X and Z are the same for all vertices but the y value is 1 off for some of them like showed in the image... anyone know the problem here? I have tried multiple different strategies to fix it and have spent plenty of time looking at the code with no success.
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Closing this duplicate post. If you have a problem with your original post then please, simply edit it.
     
Thread Status:
Not open for further replies.