Game Development Reference
In-Depth Information
Moving Textures
When creating materials in Unity you may have seen properties for x and y
offsets. These values are used to adjust the alignment and location of a texture
on a polygon's surface. If these values are adjusted constantly with each game
loop, the texture will appear animated. This is an effective way of creating an
animation that doesn't involve modeling or extra polygons. It is often used in
creating sky and water effects.
Unity Hands On
Animated Textures
Step 1. Create a new Unity project.
Step 2. In the Scene add a plane.
Step 3. Create a new material and add it to the plane.
Step 4. Find a seamless texture of your choosing from the Web.
Step 5. Drag and drop the new texture into the Project.
Step 6. Add the texture to the material. It should appear on the
surface of the plane.
Step 7. Create a new JavaScript file called animatedTexture . Add the
code in Listing 2.22 .
Listing 2.22 JavaScript to Make a Texture Scroll over the Surface
of an Object
var uvSpeed: Vector2 = new Vector2(0.0f, 1.0f );
var uvOffset: Vector2 =;
function LateUpdate()
uvOffset += (uvSpeed * Time.deltaTime );
renderer.materials[0].SetTextureOffset("_MainTex", uvOffset);
Step 8. Attach the JavaScript to the plane.
Step 9. Play. The texture will move across the surface of the plane. To
change the direction of the movement, modify the x and y values for
the uvSpeed variable in the script. The LateUpDate() function is similar
to the Update() function in that it executes for an object every game
loop. However, with LateUpDate() it occurs as the very last function
called for an object so that it can take into consideration any related
processing occurring in the same game loop.
Step 10. Open the project with the warehouse. Create a new JavaScript
file and enter the same code from Listing 2.22 . Attach this script to
polySurface437. This is the surface of the conveyor belt in the room
where you placed the curtain. Play and watch the conveyor move!
Search Nedrilad ::

Custom Search