Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Issue when changing the material texture offset

Discussion in 'General Graphics' started by samukavera, Nov 12, 2020.

  1. samukavera

    samukavera

    Joined:
    Nov 30, 2017
    Posts:
    2
    Hi guys, how are you?

    I'm trying to create an water effect changing the offset of a texture.

    There are a lot of tutorials since 2013 showing how to "scroll a sprite" and they always say to use the
    material.SetTextureOffset
    or
    material.mainTextureOffset
    but when I do this, I got an 1 pixel repeating pattern and the sprite doesn't loop...

    I'm trying with a Sprite Renderer and with a Quad with a Material Unlit/Texture on it and with this script to scroll the texture:

    Code (CSharp):
    1. public class BackgroundScript : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] private float speed = 1f;
    5.  
    6.     private float offset;
    7.  
    8.     private Material material;
    9.  
    10.     void Start()
    11.     {
    12.         // material = GetComponent<SpriteRenderer>().material;
    13.         // or
    14.         material = GetComponent<Renderer>().material;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         offset += speed * Time.deltaTime;
    20.  
    21.         // with both methods, the result is the same
    22.         // material.mainTextureOffset = new Vector2(offset, offset);
    23.         material.SetTextureOffset("_MainTex", new Vector2(offset, offset));
    24.     }
    25. }
    The result is this:


    How can I fix this?
     
    Last edited: Nov 12, 2020
  2. samukavera

    samukavera

    Joined:
    Nov 30, 2017
    Posts:
    2
    Hey guys I have figured out!

    You need to set the Wrap Mode to Repeat in the Sprite Settings to be able to repeat the material texture offset:

    upload_2020-11-19_13-7-26.png

    I hope it helps somebody :)
     

    Attached Files: