- Home /
Tiling sprite tiles only diagonally
I was trying to create a stone wall backdrop on Unity 2D. I modified a script posted by Ash-Blue on this post, however the sprites only repeat diagonally and I am not sure why.`using UnityEngine; using System.Collections;
// @NOTE the attached sprite's position should be "top left" or the children will not align properly // Strech out the image as you need in the sprite render, the following script will auto-correct it when rendered in the game [RequireComponent (typeof (SpriteRenderer))]
// Generates a nice set of repeated sprites inside a streched sprite renderer // @NOTE Vertical only, you can easily expand this to horizontal with a little tweaking public class Tiling : MonoBehaviour { SpriteRenderer sprite;
void Awake () {
// Get the current sprite with an unscaled size
sprite = GetComponent<SpriteRenderer>();
Vector2 spriteSize = new Vector2(sprite.bounds.size.x / transform.localScale.x, sprite.bounds.size.y / transform.localScale.y);
// Generate a child prefab of the sprite renderer
GameObject childPrefab = new GameObject();
SpriteRenderer childSprite = childPrefab.AddComponent<SpriteRenderer>();
childPrefab.transform.position = transform.position;
%|-2015952021_10|%
// Loop through and spit out repeated tiles
GameObject child;
for (int i = 1, l = (int)Mathf.Round(sprite.bounds.size.y); i < l; i++) {
%|-395416459_14|% child.transform.position = transform.position - (new Vector3(spriteSize.x, spriteSize.y, 0) * i); child.transform.parent = transform;
}
// Set the parent last on the prefab to prevent transform displacement
childPrefab.transform.parent = transform;
// Disable the currently existing sprite component since its now a repeated image
sprite.enabled = false;
%|-812702856_22|% }`
Answer by juicyz · Sep 07, 2016 at 11:36 PM
Please reformat your code as some of it broke. I believe your problem is:
child.transform.position = transform.position - (new Vector3(spriteSize.x, spriteSize.y, 0) * i)
You are multiplying the Vector by i and that is multiplying both x and y. You are 'moving' diagonally in that case. If you want to go left or right in a 2D game, you change only the x. If you want to go up or down in a 2D game, you change only the y.
From that post you linked, I would examine the other answers as well as they hit at your problem a bit and how you can change your code. Example:
GameObject child;
for (int i = 0, h = (int)Mathf.Round(sprite.bounds.size.y); i*spriteSize_wu.y < h; i++) {
for (int j = 0, w = (int)Mathf.Round(sprite.bounds.size.x); j*spriteSize_wu.x < w; j++) {
child = Instantiate(childPrefab) as GameObject;
child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, spriteSize_wu.y*i, 0));
child.transform.localScale = scale;
child.transform.parent = transform;
}
}
Notice the two for loops, i and j (y and x, in this case). The line of code to look at is
child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, spriteSize_wu.y*i, 0));
This is changing the x and y differently and moving it by 1 each time, going left and right, and going up and down. This is likely the solution you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why are my sprites wierd in Unity? 0 Answers
Block Perspective in Top-Down 2D Games 2 Answers
Sprites are in the wrong position in standalone, but look fine in the editor 0 Answers
2d RPG Game Sprites remake for commercial use 1 Answer
Implemening WipeOut Effect On a Sprite Using c# Script 0 Answers