Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Level Generation(Maze Game) scale image

Discussion in 'Scripting' started by Zeroxgaming84, Feb 4, 2020.

  1. Zeroxgaming84

    Zeroxgaming84

    Joined:
    Oct 14, 2017
    Posts:
    6
    Hello apologies if this has already been asked. I wrote a script that looks at a black and white image and returns the value between 0 to 1 based on pixel color. I then use that information to determine if we should spawn a prefab and where to spawn the prefab in world space. I'm having difficulties figuring out how to scale everything so that I don't end up with an object spawning at every pixel.

    example:
    image size is 64x64. goal is to control the amount of objects and how many units the level is in size.
    Best way i can describe it is for example take the scale of our solar system and shrink it down while still maintain correct positions. I attached the image i'm using to hopefully help clarify what it is i'm trying to accomplish.

    Image Being Used:

    5 by 5 orthogonal maze.png

    Script (This is in progress cause at this moment i'm still trying different ways to achieve my goal)

    public Texture2D GeneratedLevel;

    public Vector3[] pixels;
    public int imageSize;
    public float pixelValueX;
    public float pixelValueY;


    public void Start()
    {
    imageSize = GeneratedLevel.width;

    for (int x = 0; x < imageSize; x++)
    {

    pixelValueX = GeneratedLevel.GetPixel(x, 0).grayscale;
    pixelValueX = Mathf.Round(pixelValueX);

    pixels.SetValue(new Vector3(pixelValueX, 0, 0), x);
    }

    }

    }
     
  2. Zeroxgaming84

    Zeroxgaming84

    Joined:
    Oct 14, 2017
    Posts:
    6
    I would like to be able to take that image(64x64) and create a level which is 16x16 in world space.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Let me ask: How important is it that the source of this information is that image? It seems like you may be going about what may be a very simple task (storing level layouts) in an incredibly complicated, inefficient, and error-prone way (image processing). Are you expecting to create these images yourself, and if so, why have you settled on creating images, rather than scenes, prefabs, text files, etc? Are you expecting users to create their own images, and if so, how are you expecting to deal with unexpected input (how do you expect the algorithm to behave with arbitrary images)?

    Based on how this looks, I don't think you're going to be able to simply look at a pixel and spawn a prefab based on just that pixel. You've got patterns (e.g. lines/walls) that need to be recognized, especially since you don't want 64x64=4000 prefabs being spawned, you'll need to simplify it and combine objects.

    I'm not saying this can't be done, and I'll give you some guidance if you're sure you want to, but it's really difficult and almost certainly unnecessary.