Scroll 2D/3D background via texture offset

TommyE picture TommyE · Apr 29, 2016 · Viewed 13.2k times · Source

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

Answer

Programmer picture Programmer · Apr 30, 2016

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.