Reset UVs when offset exceeds a given value.

Im using this script: http://wiki.unity3d.com/index.php?title=Scrolling_UVs
and i’ve modified it to move 2 uv’s from separate materials with different speeds:

public class Movement : MonoBehaviour 
{
	public int[] materialIndex;
	public Vector2 uvAnimationRate = new Vector2( 1.0f, 0.0f );
	public string textureName = "_MainTex";
	
	Vector2 uvOffset = Vector2.zero;
	
	void LateUpdate() 
	{
		uvOffset += ( uvAnimationRate * Time.deltaTime );
		if( GetComponent<Renderer>().enabled )
		{
			GetComponent<Renderer>().materials[ materialIndex[0] ].SetTextureOffset( textureName, uvOffset * 32 );
			GetComponent<Renderer>().materials[ materialIndex[1] ].SetTextureOffset( "_CustomTexture", uvOffset );
		}
	}
}

However, i want the second texture to snap back to 0-position once it has moved for a certain distance.

What would be the easiest/least hardware-intensive way to do this?

First of all, save your Renderer component reference so you don’t Get it every frame, then keep track of two different offsets and reset second one once it’s past certain distance.

//disclaimer, written in browser:
private Renderer selfRenderer;

private Vector2 firstOffset, secondOffset, uvOffset = Vector2.zero;

public float DistanceSquared = 10f;

public void Start()
{
   selfRenderer= GetComponent<Renderer>();
}

void LateUpdate()
{
   uvOffset += uvAnimationRate * Time.deltaTime;
   firstOffset = uvOffset * 32;

   if (uvOffset.sqrMagnitude > DistanceSquared)
      secondOffset = Vector2.zero;
   else
      secondOffset = uvOffset;


   if (selfRenderer.enabled)
   {
     selfRenderer.materials[materialIndex[0]].SetTextureOffset(textureName, firstOffset);
selfRenderer.materials[materialIndex[1]].SetTextureOffset("_CustomTexture", secondOffset);
   }
}    

This will simply reset and keep the second offset at zero after it went to far.

If you want to reset it to zero but keep it moving afterwards, modify the LateUpdate code (before if conditional) with this:

uvOffset += uvAnimationRate * Time.deltaTime;
    firstOffset = uvOffset * 32;
    secondOffset += uvOffset
 
    if (uvOffset.sqrMagnitude > DistanceSquared)
       secondOffset = Vector2.zero;

Why exactly are you trying to restart the UV offset, though? I don’t see a reason to do so, as long as your texture is tiled over and over.