Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool walking; // Toggle to check if player is sending input or not.
- public string facingDirection = "null"; // Starts null to idle, but later stores the direction last input for animation processing.
- void Update ()
- {
- #region > Walk Toggle Check
- // Used to check if player is sending input or not.
- float movementInputX = Input.GetAxis ("Horizontal");
- float movementInputY = Input.GetAxis ("Vertical");
- // Used to hold last known direction faced for their
- // respective axes. Used to play directional animations.
- int faceDirectionHoldX;
- int faceDirectionHoldY;
- if(movementInputX != 0 || movementInputY != 0)
- {
- walking = true;
- }
- else
- {
- walking = false;
- }
- #endregion
- #region > Get Directions
- if(walking)
- {
- // Process incoming input and store it for
- // use later by the animator. Useful in
- // that it toggles each axis' direction
- // allowing for diagonal movement.
- if(movementInputX > 0)
- {
- // Right
- faceDirectionHoldX = 1;
- }
- else if(movementInputX < 0)
- {
- // Left
- faceDirectionHoldX = -1;
- }
- else
- {
- // No Horizontal Movement
- faceDirectionHoldX = 0;
- }
- if(movementInputY > 0)
- {
- // Up
- faceDirectionHoldY = 1;
- }
- else if(movementInputY < 0)
- {
- // Down
- faceDirectionHoldY = -1;
- }
- else
- {
- // No Vertical Movement
- faceDirectionHoldY = 0;
- }
- #region > Process Walk Direction to Animatior String
- // Take previously stored input information and
- // translate it to a direction by checking which
- // axes are active and in what direction(s).
- // Assign facingDirection a string which corrosponds
- // to the appropriate direction named in the Animator.
- if(faceDirectionHoldX == 0 && faceDirectionHoldY == 1)
- {
- facingDirection = "n";
- }
- else if(faceDirectionHoldX == 0 && faceDirectionHoldY == -1)
- {
- facingDirection = "s";
- }
- else if(faceDirectionHoldX == -1 && faceDirectionHoldY == 0)
- {
- facingDirection = "w";
- }
- else if(faceDirectionHoldX == 1 && faceDirectionHoldY == 0)
- {
- facingDirection = "e";
- }
- else if(faceDirectionHoldX == -1 && faceDirectionHoldY == 1)
- {
- facingDirection = "nw";
- }
- else if(faceDirectionHoldX == 1 && faceDirectionHoldY == 1)
- {
- facingDirection = "ne";
- }
- else if(faceDirectionHoldX == -1 && faceDirectionHoldY == -1)
- {
- facingDirection = "sw";
- }
- else if(faceDirectionHoldX == 1 && faceDirectionHoldY == -1)
- {
- facingDirection = "se";
- }
- // Play animation using facingDirection string to point
- // to the name of animations in the Animator Controller.
- anim.Play (facingDirection);
- #endregion
- }
- else
- {
- #region > Play Idle Animation
- if(facingDirection != "null")
- {
- anim.Play ("idle " + facingDirection);
- }
- #endregion
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement