- Home /
Create a straight gradient equation with a grid of 3d objects
Hey Guys,
i dont know how to solve my problem. I generate a grid full of objects. X and z axis are used to define how much rows the grid has and how much objects in a row are. What I want is to rise this grid row by row on the y axis, Like steps. I tried a lot and still have no idea how to fix this problem.
My script is using pixels to place objects with a Vector3. (because I need the z axis)
Here is it:
 using UnityEngine;
 
 public class LevelGenerator : MonoBehaviour {
 
     public Texture2D map;
     public ColorToPrefab[] colorMappings;
     public Vector3 startPos;
     public Vector3 endPos;
 
 
     void Start () {
         GenerateLevel ();
 
     }
     void GenerateLevel ()
     {
         for (int x = 0; x < map.width; x++)
         {
             for (int z = 0; z < map.height; z ++)
             {
                 GenerateTile (x, z);
             }
         }
     }
 
     void GenerateTile ( int x, int z)
     {
         Color pixelColor = map.GetPixel (x, z);
 
         if (pixelColor.a == 0)
         {
             return;
         }
 
         foreach (ColorToPrefab colorMapping in colorMappings)
         {
             if (colorMapping.color.Equals(pixelColor))
             {
                 Vector3 position = new Vector3 (x, 0, z);
                 Instantiate (colorMapping.prefab, position, Quaternion.identity, transform);
             }
         }    
     }
 }
This is creating a 3d Grid with the Prefab. The y axis is on 0 in every row, so its flat.
Hope you can help me!
 
                 
                problem.png 
                (14.7 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by CaptCommunism · Apr 06, 2018 at 08:24 AM
Solved:
Quite simple. Create a new float or int and multiply it with z.
 using UnityEngine;
 
 public class LevelGenerator : MonoBehaviour {
 
     public Texture2D map;
     public ColorToPrefab[] colorMappings;
     private float h =0.2f;
 
 
     void Start () {
         GenerateLevel ();
 
     }
     void GenerateLevel ()
     {
         for (int x = 0; x < map.width; x++)
         {
             for (int z = 0; z < map.height; z ++)
             {
                 GenerateTile (x, z);
             }
         }
     }
 
     void GenerateTile ( int x, int z)
     {
         Color pixelColor = map.GetPixel (x, z);
 
         if (pixelColor.a == 0)
         {
             return;
         }
 
         foreach (ColorToPrefab colorMapping in colorMappings)
         {
             if (colorMapping.color.Equals(pixelColor))
             {
                 Vector3 position = new Vector3 (x, z*h, z);
                 Instantiate (colorMapping.prefab, position, Quaternion.identity, transform);
             }
         }    
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                