4

I have been trying to make an infinite scrolling 2D background in Unity using a quad to display a Texture. My idea was to change the offset of the quad depending on the player's position. For some reason when I change the offset my image does not repeat properly and once an offset of 2 has been reached completely disappears.

An image of 3 different x offset values on my texture

If anyone knows how to fix this if you could get back to me it would be much appreciated.

enter image description here

Programmer
  • 121,791
  • 22
  • 236
  • 328
TommyE
  • 338
  • 4
  • 7
  • 18

1 Answers1

5

Select the original Texture not the GameOBject.

1. Change Texture Type to Texture.

2. Change Wrap Mode to Repeat.

3. Click Apply. Done!

enter image description here

Latest version of Unity menu for Textures has changed. See the below image:

enter image description here

Now to animate the texture from script,

1. Create a Quad GameObject -> 3D Object ->Quad. Scale the Quad to the size you want

2. Create a light. GameObject->Light->Directional Light. You can adjust the light Intensity to whatever you like.

3. Drag your Texture/Sprite to the Quad in the Scene View.

Now for your script:

public GameObject quadGameObject;
private Renderer quadRenderer;

float scrollSpeed = 0.5f;

void Start()
{
    quadRenderer = quadGameObject.GetComponent<Renderer>();
}

void Update()
{
    Vector2 textureOffset = new Vector2(Time.time*scrollSpeed,0);
    quadRenderer.material.mainTextureOffset = textureOffset;
}

For 2D, you can also use a Plane or Quad from the GameObject ---> 3D Object menu and the code above should work fine.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks Programmer my background is scrolling properly now ;) – TommyE Apr 30 '16 at 11:53
  • @TommyEaves You are welcome. I was wondering if it worked or not. – Programmer Apr 30 '16 at 11:59
  • @TommyEaves No problem. – Programmer Apr 30 '16 at 12:01
  • ah wrap mode wasn't on! brilliant, @Programmer – Fattie May 02 '16 at 23:08
  • 1
    @JoeBlow It could be just that but no one knows how he was also moving the texture and didn't want to hear that it was not working too so I threw both the Texture and the moving code part together just in-case he was doing any of them wrong. Based on the picture he provided, it looks like wrap mode problem.Thanks. – Programmer May 02 '16 at 23:47
  • Note that not all Unity built-in shaders work, although they have offset exposed as editable. The ones I could successfully test were "Unlit/..." (Unity 2019.1) – mireazma Nov 17 '19 at 13:19