Game Development Reference
In-Depth Information
Go ahead, give it a try, and trust me, you'll like the results! Open up the same switch
statement as the previous one and add some code so it now looks like the following
code. Note that we are adding a new Vector3 object at the top to grab and store the
localScale of the object.
// Grab the current localScale of the object so we have
// access to it in the following code
Vector3 localScale = transform.localScale;
switch(newState)
{
case PlayerStateController.playerStates.idle:
animation.Play("idleAnimation");
break;
         
case PlayerStateController.playerStates.left:
// Play the Run Animation when the player is moving Left animation .
Play("runAnimation");
                   
if(localScale.x > 0.0f)
{
localScale.x *= -1.0f;
transform.localScale  = localScale;
}
break;
              
case PlayerStateController.playerStates.right:
// Play the Run Animation when the player is moving Right
animation.Play("runAnimation");
if(localScale.x < 0.0f)
{
localScale.x *= -1.0f;
transform.localScale = localScale;              
}
break;
It really is as easy as that. Now play the game and you'll see the player object facing
the correct direction while moving. You now have an animated player capable of
moving left and right, updating its orientation and its animations, and doing all of
this along a platform!
 
Search WWH ::




Custom Search