using UnityEngine; /****************************************************************************** * This class provides basic movement controls to a player sprite in the scene. * It responds to the basic keyboard movement commands and will use the sprite * and animator to face the object in the correct direction as it moves. * * SETUP * SpriteRenderer * Start by setting up the player object using a SpriteRenderer, easily done by * dragging the image into the scene. * * RigidBody2D * This script moves the player object on screen by interacting with the * rigidbody2d component. Don't forget to set the Gravity Scale to 0 and check * the Freeze Rotation Z option. If you forget the object will slowly slide * down the screen. * * Animator * Add an animator component to the player object. The referenced Animator * controller will have the walking, running, etc animations setup in mecanim * For the purpose of this script, you will need to configure the following * parameters in the controller and then set the animation transitions to * match. * - yMove : Integer * - xMove : Boolean * - moving : Boolean * * Finally, attach this script to the player object. At this point, create a * prefab out of the object. * * Enhancements: * - Add mouse click control to the movement. ******************************************************************************/ public class CharacterMovement : MonoBehaviour { /************************************************************************** * These variables setup references to the player object in the game scene. * At a minimum, the player object needs to be a 2D sprite and have a 2D * RigidBody for this script to work. The animation component is not * critical to the script, but the script will attempt to set animation * variables so that the player image will appear to move on the screen. **************************************************************************/ private SpriteRenderer playerSpriteImage; private Rigidbody2D playerRigidBody2D; private Animator playerAnim; /************************************************************************** * These variables manage the player's movement on screen. Speed can be set * in the Unity IDE or in code. **************************************************************************/ //Variable to track how much movement is needed from input private float movePlayerHorizontal; private float movePlayerVertical; private Vector2 movement; // Speed modifier for player movement public float speed = 4.0f; /// /// This method is connecting the script to the components of the player object /// in the scene. The player object should have each of these components added /// along with this script to make up a complete player. /// void Awake() { playerRigidBody2D = (Rigidbody2D)GetComponent(typeof(Rigidbody2D)); playerAnim = (Animator)GetComponent(typeof(Animator)); playerSpriteImage = (SpriteRenderer)GetComponent(typeof(SpriteRenderer)); } /// /// The update method does the work of moving the player object on the screen /// in the desired direction. It also uses the animation and sprite components /// to "face" the player in the right direction. /// void Update() { // The first step is to get the player's input to determine which direction // to move the player object, then use speed to set the movement. movePlayerHorizontal = Input.GetAxis("Horizontal"); movePlayerVertical = Input.GetAxis("Vertical"); movement = new Vector2(movePlayerHorizontal, movePlayerVertical); playerRigidBody2D.velocity = movement * speed; // This section handles the animation control to make sure that the player // object is facing in the right direction during movement. It is basically // setting animation variables that should be setup in the mecanim library. // Mecanim does the work of dislaying animations. if (movePlayerVertical == 0 && movePlayerHorizontal == 0) { playerAnim.SetBool("moving", false); } else { playerAnim.SetBool("moving", true); } if (movePlayerVertical != 0) { playerAnim.SetBool("xMove", false); playerSpriteImage.flipX = false; if (movePlayerVertical > 0) { playerAnim.SetInteger("yMove", 1); } else if (movePlayerVertical < 0) { playerAnim.SetInteger("yMove", -1); } } else { playerAnim.SetInteger("yMove", 0); if (movePlayerHorizontal > 0) { playerAnim.SetBool("xMove", true); playerSpriteImage.flipX = false; } else if (movePlayerHorizontal < 0) { playerAnim.SetBool("xMove", true); playerSpriteImage.flipX = true; } else { playerAnim.SetBool("xMove", false); } } if (movePlayerVertical == 0 && movePlayerHorizontal == 0) { playerAnim.SetBool("moving", false); } else { playerAnim.SetBool("moving", true); } } }