Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Schytheron · Jul 23, 2015 at 11:41 AM · unity 52dplatformeroverlaptiling

Im trying to do 2D tiling but sprites keep overlapping!

Im very new to Unity and there are alot of things I dont know about it, but today I tried doing tiling on my own (without looking up how to actually do it... im making a 2D platformer btw) and everything is working fine... except one thing! When my camera view reaches the end of my ground sprite it Instantiates a copy of my ground sprite to the right. But instead of putting the X value of the new sprite by the edge of the old ground sprite (to have a perfect infinite loop) it overlaps the old one even though I clearly stated the right position in my code. You can visually see the change of sprite pattern on the ground when you pass a certain point.

I uploaded a Youtube video to show the issue:https://youtu.be/HckwD_yDk1o (watch the game view carefully when the camera in the scene view gets to the edge of the sprite)

Here is my code: using UnityEngine; using System.Collections;

 public class Tiling : MonoBehaviour {
 
     private GameObject newBuddy;
     private float edgeRightXpos;
     private Vector3 horzExtent;
     private SpriteRenderer sprite;
 
     void Awake () {
         newBuddy = GameObject.Find ("ForegroundDirt");
         sprite = newBuddy.GetComponent<SpriteRenderer> ();
     }
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         edgeRightXpos = newBuddy.transform.position.x + sprite.bounds.extents.x;
         horzExtent = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, Camera.main.nearClipPlane));
 
         if (horzExtent.x >= edgeRightXpos) {
             Vector3 newBuddyPosition = new Vector3(edgeRightXpos,newBuddy.transform.position.y,newBuddy.transform.position.z);
             newBuddy = Instantiate(newBuddy, newBuddyPosition ,newBuddy.transform.rotation) as GameObject;
             newBuddy.transform.parent = GameObject.Find("ForegroundDirt").transform;
 
         }
     }
 }
 
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

Answer by NoseKills · Jul 23, 2015 at 12:44 PM

You are always adding extents.x to the previous dirt graphic x and placing a new one there ? Because extents is half of the sprite size, that means the center of the new image will be at the end of the previous one and half of them will overlap. You should probably use Bounds.size

Comment
Add comment · Show 5 · 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
avatar image Schytheron · Jul 23, 2015 at 01:23 PM 0
Share

Ooh! Thank you! Im gonna try it out and see if it works. I didn't know what extents actually did. I just used it because it did not work when I used Bounds.Size before, but now my code looks abit different from last time.

avatar image Schytheron · Jul 23, 2015 at 01:40 PM 0
Share

Damn it! It did not work when I put Sprite.bounds.size.x. Now nothing happens.

I checked and watched the variables edgeRightXpos and horzExtent and when the right side of the camera view gets to the edge of the sprite horzExtent has a value of around 45 while edgeRightXpos has a value of around 90.

For some reason when I put Bounds.size.x it gives me double the width of the actual sprite.

avatar image Bunny83 · Jul 23, 2015 at 02:38 PM 0
Share

@Schytheron You need to use "bounds.extents" for your edge checking, but the position of the new sprite has to be bounds.size ahead the old position. The pivot of your sprite is usually in the center. Since you place the new sprite's pivot at the edge of the old one they will overlap half of their size.

You might want to use a sheet of paper and draw your screen rectangle and the two sprite rectangles with their pivots so you can see the distances required.

avatar image Schytheron · Jul 23, 2015 at 03:11 PM 0
Share

@Bunny83 I did it! But not using bounds.size. Ins$$anonymous$$d I added + bounds.extents to the old position.

ins$$anonymous$$d of: Vector3 newBuddyPosition = new Vector3(edgeRightXpos,newBuddy.transform.position.y,newBuddy.transform.position.z);

I wrote:

  Vector3 newBuddyPosition = new Vector3(edgeRightXpos + sprite.bounds.extents.x,newBuddy.transform.position.y,newBuddy.transform.position.z);


avatar image Bunny83 · Jul 23, 2015 at 04:11 PM 0
Share

Sure, it's exactly the same thing. Since "edgeRightXpos" is actually "newBuddy.transform.position.x + sprite.bounds.extents.x" you effectively do:

 newBuddy.transform.position.x + sprite.bounds.extents.x + sprite.bounds.extents.x;

Since bounds.size is always 2 times the extend it's the same as:

 newBuddy.transform.position.x + sprite.bounds.size.x;

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to prevent jitter with movement over curved slopes 0 Answers

2D Platformer moving platform help C# 3 Answers

Unity 2D: Reversed Gravity doesn't work 0 Answers

How to make a looping room on 2d platformer? 0 Answers

My app freezes on specific y coordinates 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