I am trying to create a moving 2d background for a platformer by offsetting the texture of a Quad (mainTextureOffset). The offset moves to match the camera movement, it sort of works, but instead of the texture tiling, i get some wacky effect. Anyone know what I am doing wrong?
The code i am using is in a BackgroundScroller class attached to the background component.
public class BackgroundScroller : MonoBehaviour {
public static BackgroundScroller current;
private float lastCameraPos;
void Start () {
lastCameraPos = Camera.main.transform.position.x;
}
void Update () {
float shift = Camera.main.transform.position.x - lastCameraPos;
lastCameraPos = Camera.main.transform.position.x;
Vector2 offset = renderer.material.mainTextureOffset;
offset.x += +shift * 0.01f;
renderer.material.mainTextureOffset = new Vector2 (offset.x, 0);
}
}
Do i need to check the offset.x on update and move the texture manually once its off screen?
Thanks