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:- YouTube
(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;
}
}
}