Scrolling texture

Hello!

I’m working on a side scrolling game in which the road and few layers of background have scrolling texture to make the ilusion of movement along x axis.
Here is the script I made for this:

void Update () {
        float newX = myRenderer.material.GetTextureOffset ("_MainTex").x + step;
        float newY = myRenderer.material.GetTextureOffset ("_MainTex").y + step;

        Vector2 newOffset = Vector2.Lerp (myRenderer.material.GetTextureOffset ("_MainTex"), new Vector2 (newX, newY), Time.deltaTime * speed * GameManager.Instance.globalScrollSpeed);

        myRenderer.material.SetTextureOffset ("_MainTex", newOffset);
    }

And this works perfectly for about a minute or so, but after that, the texture begins staggering. I think because the value of the texture offset is constantly increasing and reaches really high values.
I used something like this to fix it, but it made it worse:

        if (newX > 1)
            newX -= 1;

        if (newY > 1)
            newY -= 1;

Anyone has an ideea how to fix for this?

Some video or screenshot would be great to get behind your error. :slight_smile: So what is your “step” value also would be great to understand the code.

  • step is 0.1f
  • here is a short clip: https://vid.me/zrdl - pay close attention to those poorly drawn hills in full screen. It might be difficult to see that jitter effect since it was recorded on my PC and runs smoother, but on a device is really obvious and persistent.
  • I use the unlit - texture and transparent shaders and here is how the scene looks:

Hm, okay. This is a 4 second clip, but you said, it starts to jitter or sth. about after 1 minute. You could send me your scene, if you like. So I can check out, if there is a problem on my system, or you could do a webplayer demo just with the animation, so I can watch this for more than 4 seconds :slight_smile:

Simplified your code a bit:

Material mat;
    void Start () {
        mat = GetComponent<Renderer>().material;
    }
    void Update () {
        mat.mainTextureOffset = mat.mainTextureOffset + step * Time.deltaTime * speed * GameManager.Instance.globalScrollSpeed;
    }

If your speed is constantly increasing then it may get to a point where it’s so fast that it appears to be jittering, but really it’s just so fast that you can’t see it properly.

Depending on your video system, it may only accept texture offsets up to a certain amount. You may need to use the modulo operator to keep it within acceptable limits.