0

I've made a tiledrawer, which takes a list of sprites, and then randomly picks one of those sprites, and draws it in a 2D grid.

This works, but I have a feeling I'm not doing it the right way. When the camera moves over the area, the tiles look choppy.

My code for the generator:

public GameObject tileType;
public Sprite[] sprites;
private Grid grid;
public int gridRadius = 100;

// Start is called before the first frame update
void Start()
{
    if (sprites == null || sprites.Length == 0)
    {
        return;
    }
    var tileSize = 0.5f;
    var tileScale = 1.56f;

    var half = gridRadius / 2;
    var beginX = half * tileSize * -1;
    var beginY = half * tileSize * -1;

    var beginOffset = new Vector3(beginX, beginY, 0);

    grid = GameObject.FindObjectOfType<Grid>();
    var offset = new Vector3(0.25f, 0.25f, 0) + beginOffset;

    for (int x = 0; x < gridRadius; x++)
    {
        for (int y = 0; y < gridRadius; y++)
        {
            var tileOffset = offset + new Vector3(tileSize * x, tileSize * y, 0);

            var tile = Instantiate(tileType, transform.position + tileOffset, Quaternion.identity, grid.transform);
            tile.transform.rotation = transform.rotation;

            var renderer = tile.GetComponent<SpriteRenderer>();
            tile.transform.localScale = new Vector3(1.5f, 1.5f, 0);
            renderer.transform.localScale = new Vector3(tileScale, tileScale, 0);
            renderer.sprite = RandomSprite();
        }
    }

}

private Sprite RandomSprite()
{
    var rand = Random.Range(0, sprites.Length - 1);
    return sprites[rand];
}

// Update is called once per frame
void Update()
{

}   

When I say "choppy", I mean the tiles sometimes generates lines between them, sometimes not, even though there is not space between them.

enter image description here

What is the right way of drawing a grid of tiles, from a list of sprites?

WynDiesel
  • 103
  • 3

1 Answers1

1

In my opinion there are a couple different things this could be.This is one of those things that will just require some general troubleshooting to figure out. But these are three things I would try.
    1 Texture
It could just be that the image isn't tile-able. It looks like you are picking a random sprite from an array of sprites so maybe certain sprites tile well with others but not in certain scenarios.
    2 Compression
Could be as simple as needing to adjust the sprite compression settings on the sprite importer. Sometimes unity's compression algorithms leave images with unexpected artifacts.
    3 Too much scaling
This isn't so much a cause of the problem but its a problem that should be addressed.I don't think you should be playing around with scaling like that in script unless you have a very good reason. It seems like when I have written or seen code similar to this it is because there is import settings that need to change or scaling issues further up the line. If you need to scale you must compartmentalize(i.e. animation) where when you are done scaling an object you should reset it back to its original scale or vector3.one.One last note I will make about this is the tile size/ scale shouldn't be hard coded. You will probably end up regretting this in the future.
Let me know if any of these helped.