Game Development Reference
In-Depth Information
9. Now, save it and go back to Unity; drag-and-drop our script to our
Player
, then
click on
Player
and go to the
Inspector
window. Click on the
Loop Sprites
, and set
Size
to
2
, then set the following:
Element 0
:
F
Sprite Texture
:
s_set
F
In_frame Per Sec
:
2
F
In_grid X
:
2
F
In_grid Y
:
1
Element 1
:
F
Sprite Texture
:
w_set
F
In_frame Per Sec
:
8
F
In_grid X
:
4
F
In_grid Y
:
2
We are done. Let's click on the
play
buton to play the game. We will see our
Player
moving
his hand back and forth. Next, press the
A
key or
¬
key,
D
key or
®
key to move the
Player
to
the left or to the right; now we see that he is walking. Isn't that cool?
Objective Complete - Mini Debriefing
We just created a script that controls the movement of our character, and his animaion.
First, we set
in_direction
to
1
because we want our character To start by facing the right-
hand side. Then, we are looping through the array and iniializing the
SpriteManager
class
from its length. We will get the main camera from the current scene by using
Camera.main
.
This syntax allows us to access the
Main Camera
object from anywhere we want, and then
we assign the main camera posiion point to our character. Next, we will put the script in the
Update()
funcion, which is already created by default —similar to the
Start()
funcion.
This funcion will be used to control our character movement from walking to jumping, and
for updaing the animaion.