Game Development Reference
In-Depth Information
else if (movePlayerVector < 0 && facingRight)
{
Flip();
}
}
void Flip()
{
// Switch the way the player is labeled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
The script is fairly basic; it simply has some parameters to control the speed and its facing
direction. The update method checks if the player is controlling the game using the de-
fault horizontal keys (left and right) and then applies force to move the hero accordingly.
Finally, we check which way the hero is facing, and based on the direction the player has
pressed, it will flip the sprite to face the correct direction.
Tip
The scale parameters are a common way to swap which way a sprite is drawn toward.
This means you don't need sprites for every single angle of your game; you can just flip or
rotate them.
To finish off, add the script to the player's game object by either dragging it to the object
in the hierarchy or navigating to Add Component | Scripts | Character Movement .
Note
You should note that this very simple controller code only uses a keyboard input. For
touch-only-based platforms such as iOS, WP8, and arguably Android, you would need to
include touch controls or use the accelerometer.
Search WWH ::




Custom Search