Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scrolling texture

Discussion in 'Scripting' started by laur-ganta, Apr 13, 2015.

  1. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    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:

    Code (CSharp):
    1. void Update () {
    2.         float newX = myRenderer.material.GetTextureOffset ("_MainTex").x + step;
    3.         float newY = myRenderer.material.GetTextureOffset ("_MainTex").y + step;
    4.  
    5.         Vector2 newOffset = Vector2.Lerp (myRenderer.material.GetTextureOffset ("_MainTex"), new Vector2 (newX, newY), Time.deltaTime * speed * GameManager.Instance.globalScrollSpeed);
    6.  
    7.         myRenderer.material.SetTextureOffset ("_MainTex", newOffset);
    8.     }
    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:
    Code (CSharp):
    1.  
    2.         if (newX > 1)
    3.             newX -= 1;
    4.  
    5.         if (newY > 1)
    6.             newY -= 1;
    Anyone has an ideea how to fix for this?
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Some video or screenshot would be great to get behind your error. :) So what is your "step" value also would be great to understand the code.
     
  3. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    • 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:
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    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 :)
     
  5. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    Simplified your code a bit:
    Code (CSharp):
    1. Material mat;
    2.     void Start () {
    3.         mat = GetComponent<Renderer>().material;
    4.     }
    5.     void Update () {
    6.         mat.mainTextureOffset = mat.mainTextureOffset + step * Time.deltaTime * speed * GameManager.Instance.globalScrollSpeed;
    7.     }
    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.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    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.