Hello all,
I have waterfalls in my project which have 3 different planes of geometry, scrolling their UVs at different intervals, in order to achieve the effect I want. To do this I have been using the UV Scrolling code from the wiki, and I attached the script three times to each waterfall (one for each material index).
Here is the original code:
using UnityEngine;
using System.Collections;
public class uvScroll : MonoBehaviour
{
public int materialIndex = 0;
public Vector2 uvAnimationRate = new Vector2( 1.0f, 0.0f );
public string textureName = "_MainTex";
Vector2 uvOffset = Vector2.zero;
void LateUpdate()
{
uvOffset += ( uvAnimationRate * Time.deltaTime );
if( renderer.enabled )
{
renderer.materials[ materialIndex ].SetTextureOffset( textureName, uvOffset );
}
}
}
Desiring a clean project, I attempted to combine the functions of the three into one simple script, but my scripting skills are horribly lacking and the UVs end up being scrolled at a rate equal to the sum of all 3 floats, rather than each material index scrolling at its own rate.
Here is my modified code:
using UnityEngine;
using System.Collections;
public class uvScroll_waterfall : MonoBehaviour
{
public int materialIndex0 = 0;
public int materialIndex1 = 1;
public int materialIndex2 = 2;
public Vector2 uvAnimationRate0 = new Vector2( 0.0f, 1.25f );
public Vector2 uvAnimationRate1 = new Vector2( -0.15f, 1.0f );
public Vector2 uvAnimationRate2 = new Vector2( 0.15f, 0.75f );
public string textureName0 = "_BumpMap";
public string textureName1 = "_MainTex";
public string textureName2 = "_MainTex";
Vector2 uvOffset = Vector2.zero;
void LateUpdate()
{
uvOffset += ( uvAnimationRate0 * Time.deltaTime );
if( renderer.enabled )
{
renderer.materials[ materialIndex0 ].SetTextureOffset( textureName0, uvOffset );
}
uvOffset += ( uvAnimationRate1 * Time.deltaTime );
if( renderer.enabled )
{
renderer.materials[ materialIndex1 ].SetTextureOffset( textureName1, uvOffset );
}
uvOffset += ( uvAnimationRate2 * Time.deltaTime );
if( renderer.enabled )
{
renderer.materials[ materialIndex2 ].SetTextureOffset( textureName2, uvOffset );
}
}
}
Can a more experienced C# user tell me how to structure this so that each material scrolls at it’s own rate?
Many thanks for any help or suggestions!