2019-06-01 net

(Brent) #1

PROJECTS
Unity


Unity. We have one for each sky sprite. We also
added a public float data type to hold the speed of
our animation. Notice for floats in C#, we use the ‘f’
letter after the value.

STEP 6: LINK GAME OBJECTS
In Unity, drag your sky1 sprite into the box labelled
sky1 and sky2 into the box for sky2. This will link
those two sprites to your script component so you
can access them via code.

STEP 7: ANIMATE THE SKY
Go back into the Visual Studio editor to continue
working on the SkyAnimator script. You need to move
the sky sprites left a little on each frame. Add the
following code inside the Update function to do that:

sky1.transform.position = new Vector3 (sky1.transform.
position.x - speed, transform.position.y, transform.
position.z);
sky2.transform.position = new Vector3 (sky2.transform.
position.x - speed, transform.position.y, transform.
position.z);

This updates the position of the sky sprites,
contained within a transform object – a core
component of every game object. Positions are
vector3 types ( x,y,z ). We update the position to be
the same as it was with the only adjustment being to
subtract the speed value from the x position.
If you test your code now, you’ll see the sky pieces
animate horizontally to the left. This goes perfectly
until they animate off the screen and out of sight.

STEP 8: ENDLESS SCROLLING
To stop the sky from scrolling off screen, you need
to swap each sky piece back to a starting position as
it scrolls off the screen. To do this, add the following
code after the last code you entered:

if (sky1.transform.position.x <= -19.0) {
sky1.transform.position = new Vector3 (19.0f,
transform.position.y, transform.position.z);

}
if (sky2.transform.position.x <= -19.0) {
sky2.transform.position = new Vector3( 19.0f,
transform.position.y, transform.position.z);
}

In this code, you are checking to see if the sky sprite
has moved to less than -19 along the x-axis. That
value was the far-left position you set the sky1 sprite
to in the main scene. If your x position was different
than mine, use yours instead of -19. Then we are
setting the x position to the same distance but in the
positive, to reset it. This tile swapping technique is a
great approach for endless scrolling games because
you can use very few sprites to create the effect.

STEP 9: STOP FUNCTION
Before we leave this class, let’s add one last function
that we’ll need later. It will stop the movement of the
sky when we need to pause or stop the game.

public void stop(){
speed = 0.0f;
}
// c o d e //

Now when you run your code, you should see your
sky scroll by endlessly! You can adjust the speed
using the public speed property we created in the
Component inspector. You can swap out images for
your sky and follow this model to create any 2D side-
scroller endless background animation. You could
also update the speed of the scrolling based on game
factors such as the speed of the character, increased
difficulty or different environments.

STEP 10: ADD A PLAYER CHARACTER
You need to add a character. If you haven’t already,
import an image to use for a sprite. Convert the
image to a sprite as you did before and then drag a
copy of it from your assets to the scene.
Once you have a character sprite in the scene,
rename it ‘plane’ in the Inspector or in the Hierarchy

Top left Add a script
component in the Inspector
window
Top right Adding and size
the player character to
about 5% of height
Free download pdf