Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Qauliot · Sep 06, 2016 at 07:13 PM · 2d gamespritestiling

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|% }`

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges