- Home /
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;
}
}
}
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
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.
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.
@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.
@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);
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
Follow this Question
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