Game Development Reference
In-Depth Information
3. Now, we will see three windows in the
MonoDevelop
screen:
On the top-left is
Solution
; we can see our project folder here, but it will
only show the folder that contains a script.
On the botom-let, we will see a
Document Outline
; this window will show
all the funcions, classes, and parameters in the ile.
The last window on the right will be used to type our code.
4. Let's get our hands dirty with some code—first create the
CharacterController_2D
class. At present, we are creaing parameters:
public var f_speed : float = 5.0;
public var loopSprites : SpriteManager[];
private var in_direction : int;
f_speed
is the speed of our character, and we set it to
public
so we can adjust
it inside the Unity editor. The array
loopSprites
of the
SpriteManager
class
will control the update of our sprite animaion texture, which we will create later.
in_direction
tracks the direcion of our character, which will return only 1
(right direcion) or
-1
(let direcion).
5. Next, we will include the script in the
Start()
funcion, which is already created
by default:
public function Start() : void {
in_direction = 1;
//Initialization Sprite Manager
for (var i : int = 0; i<loopSprites.length; i++) {
loopSprites[i].init();
}
//Update Main Camera to the character position
Camera.main.transform.position = new Vector3(transform.
position.x, transform.position.y, Camera.main.transform.
position.z);
}
6. Next, we will include the script in the
Update()
funcion, which is already created
by default similar to the
Start()
funcion:
// Update is called once per frame
public function Update () : void {
if (Input.GetButton("Horizontal")) {
//Walking
in_direction = Input.GetAxis("Horizontal") < 0 ? -1: 1;
rigidbody.velocity = new Vector3((in_direction*f_speed),
rigidbody.velocity.y, 0);
//Reset Stay animation frame back to the first frame
loopSprites[0].resetFrame();