How to create tiled map correctly?
Hey guys, I am newbie in Unity, I start to develop a Unity project TODAY. Sorry about the stupid question I ask, but I really don't know where I do wrong.
first, I follow this tutorial and try to create a tiled map. Then I have a C# script called GameManager which will create the tiled map by the following code:
int rows = 5;
int columns = 10;
private Transform shopHolder;
void initDirts() {
shopHolder = new GameObject ("Shop").transform;
for (int x=0; x<columns; x++) {
for(int y=0; y<rows; y++ ) {
GameObject obj = dirts[Random.Range(0, dirts.Length)];
GameObject instance = Instantiate(obj, new Vector3(x,y,0f), Quaternion.identity) as GameObject;
instance.transform.parent = shopHolder;
Debug.Log(instance);
}
}
}
But when I run the project to see result, it shows like this:
The position of each tile is too far, I expect them should be stay together. But I don't know how to do that. Can someone give some direction?
Here is the GameObject in prefab and its setting in my Unity project, each of the sprite size is 32x32 pixels.
I seems knows the reason. Yesterday, I download the tutorial's assests from Unity Store. And I change the tile sprite source to the tutorial's sprite sheet. Then it looks fine!
As the screenshot, the left one is my own sprite sheet, the right one is tutorial's sprite sheet. Both of the sprite I using is 32x32 pixels. The difference is, I create my sprite sheet by TexturePacker.
When I drag the sprite I use for tile to the scene. You can see the difference, the sprite from my own sprite sheet is much smaller. The tutorial's sprite just fits a grid size. I really don't know why this happen????
Answer by firestoke · Oct 22, 2015 at 05:22 AM
oh my god! I know why......................
Because the sprite sheet generated by TexturePacker, the default value of "Pixels Per Unit" is 100. However, the value of tutorial's sprite sheet is 32.
I change the value from 100 to 32, then everything is fine now!
I am really newbie in Unity... Orz. Sorry about this stupid question again. =.,=
Answer by Jessespike · Oct 21, 2015 at 05:20 PM
Pixels and World Space units aren't the same. So you need to either resize the sprites or change their position when they are instantiated.
GameObject instance = Instantiate(obj, new Vector3(x,y,0f), Quaternion.identity);
instance.transform.localScale = new Vector3(4f, 4f); // change the scale in runtime
float positionScale = 0.25f; // scale the distance between sprites
GameObject instance = Instantiate(obj, new Vector3(x*positionScale ,y*positionScale ,0f), Quaternion.identity) as GameObject;
Alternatively, you can use a Quad. Since quads are sized for world space, I mean, you wont have to scale anything, quads can be aligned in a simple loop like in your example.
Your answer
Follow this Question
Related Questions
How To Change Which Edge Tiled Images Snap To? 0 Answers
Can Tiled place prefabs instead of just textures? 0 Answers
unity ui darker than actual assets 0 Answers
3DS object applied material shown repeated 0 Answers
Best way to generate tiled ground? 0 Answers