Game Development Reference
In-Depth Information
Listing 3.5 Script to Swap Frames on a Texture Atlas According
to a Set Frame Rate
var textures: Texture2D[];
...
function Start()
{
timeBetweenFrames = 1.0/framerate;
...
}
function LateUpdate ()
{
if(Time.time > nextFrame)
{
renderer.material.mainTextureOffset =
Vector2 (currentFrame*textures[0].width/
totalWidth,0);
nextFrame = Time.time + timeBetweenFrames;
currentFrame++;
if(currentFrame > = textures.length)
{
currentFrame = 0;
}
}
}
Step 12. Play. The sprite will be animated.
Step 13. To change the speed at which the animation plays, select
the plane in the Hierarchy and locate the Framerate variable of the
AnimatedSprite component in the Inspector. This value controls the
number of frames drawn per second. Change it to 100 to see the
animation play faster and change it to 1 to see it run slower.
Step 14. Download Chapter Three/background.psd from the Web site.
Add a new plane to the scene behind the sprite and put this new texture
on it. It is a city background scene. Stretch the plane out to accommodate
the texture. Set the material shader to Sprite to make it display correctly
without lighting. If the image is upside down, rotate the plane by 180°
around the y axis.
Step 15. Create a new JavaScript file named scrollBackground . Add the
image scrolling code from Chapter Two, repeated here in Listing 3.6 for
your convenience. Attach the JavaScript to the new plane.
Step 16. With this new plane selected, change the UVSpeed to (0.1,0) in
the Inspector.
Step 17. Play. The background will scroll, making the character appear to
walk along the street.
Search Nedrilad ::




Custom Search