Game Development Reference
In-Depth Information
To animate the Offset for the main texture and bump maps, you can use
Time.deltaTime
and the
speed to control the Offset.
Change the
Update
function to a
FixedUpdate
function.
8.
Add the following in the
FixedUpdate
function for the main texture:
9.
// texture offset variables
float offsetU1 = Time.time * -scrollSpeedU1;
float offsetV1 = Time.time * -scrollSpeedV1;
// animate the UVs
if (animateUV) { // if the flag to animate the texture is true...
Vector2 offset1 = new Vector3(offsetU1,offsetV1);
theMaterial.SetTextureOffset ("_MainTex",offset1);
}
You have no need for animating a bump texture on the laser, but animating a materials bump texture
can produce some very interesting water-type effects.
10.
Add the following to change the bump texture offset value:
// bump texture offset variables
if (animateBump) {
float offsetU2 = Time.time * -scrollSpeedU2;
float offsetV2 = Time.time * -scrollSpeedV2;
if (animateUV) { // if the flag to animate the texture is true...
Vector2 offset2 = new Vector3(offsetU2,offsetV2);
renderer.materials[materialIndex].SetTextureOffset ("_BumpMap",offset2);
}
}
You can access the material through the renderer component with
renderer.material
if there is only
one material, or
renderer.materials[
index number
]
if there might be more than one. Setting the
index number to 0 will use the first material unless otherwise specified.
The trickiest part about this script is discovering what the
internal name
of the shader parameter is in the
script, so you can feed it into the
SetTextureOffset
method. Unlike regular variables/parameters, those
used in shaders do not necessarily coincide. A good place to begin your search for internal shader
parameter names is the UnityAnswers post,
http://answers.unity3d.com/questions/501797/
can-i-get-the-a-list-of-the-properties-in-a-shader.html
, where robertbu has generated a list
of internal names for several of the Unity shaders. If you are feeling adventurous, you may want
It contains the source code for each of the built-in shaders and takes the guess work out
of identifying the correct parameter names. Once you track down the shader you are looking for,
spotting the names is easy.
11.
Save the script.
12.
Add the UVAnimator script to the Laser Point object.
13.
Set the Scroll Speed U1 to
5
.
14.
Click Play, and check out the subtle pulsing effect.