Advertisement
Guest User

Blend Tree Workaround

a guest
Jan 11th, 2014
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. public bool walking; // Toggle to check if player is sending input or not.
  2. public string facingDirection = "null"; // Starts null to idle, but later stores the direction last input for animation processing.
  3.  
  4. void Update ()
  5. {
  6.         #region > Walk Toggle Check
  7.  
  8.         // Used to check if player is sending input or not.
  9.  
  10.         float movementInputX = Input.GetAxis ("Horizontal");
  11.         float movementInputY = Input.GetAxis ("Vertical");
  12.  
  13.         // Used to hold last known direction faced for their
  14.         // respective axes. Used to play directional animations.
  15.  
  16.         int faceDirectionHoldX;
  17.         int faceDirectionHoldY;
  18.  
  19.         if(movementInputX != 0 || movementInputY != 0)
  20.         {
  21.             walking = true;
  22.         }
  23.         else
  24.         {
  25.             walking = false;
  26.         }
  27.  
  28.         #endregion
  29.  
  30.         #region > Get Directions
  31.  
  32.         if(walking)
  33.         {
  34.             // Process incoming input and store it for
  35.             // use later by the animator. Useful in
  36.             // that it toggles each axis' direction
  37.             // allowing for diagonal movement.
  38.  
  39.             if(movementInputX > 0)
  40.             {
  41.                 // Right
  42.                 faceDirectionHoldX = 1;
  43.             }
  44.  
  45.             else if(movementInputX < 0)
  46.             {
  47.                 // Left
  48.                 faceDirectionHoldX = -1;
  49.             }
  50.  
  51.             else
  52.             {
  53.                 // No Horizontal Movement
  54.                 faceDirectionHoldX = 0;
  55.             }
  56.  
  57.             if(movementInputY > 0)
  58.             {
  59.                 // Up
  60.                 faceDirectionHoldY = 1;
  61.             }
  62.  
  63.             else if(movementInputY < 0)
  64.             {
  65.                 // Down
  66.                 faceDirectionHoldY = -1;
  67.             }
  68.  
  69.             else
  70.             {
  71.                 // No Vertical Movement
  72.                 faceDirectionHoldY = 0;
  73.             }
  74.  
  75.             #region > Process Walk Direction to Animatior String
  76.  
  77.             // Take previously stored input information and
  78.             // translate it to a direction by checking which
  79.             // axes are active and in what direction(s).
  80.             // Assign facingDirection a string which corrosponds
  81.             // to the appropriate direction named in the Animator.
  82.  
  83.             if(faceDirectionHoldX == 0 && faceDirectionHoldY == 1)
  84.             {
  85.                 facingDirection = "n";
  86.             }
  87.             else if(faceDirectionHoldX == 0 && faceDirectionHoldY == -1)
  88.             {
  89.                 facingDirection = "s";
  90.             }
  91.             else if(faceDirectionHoldX == -1 && faceDirectionHoldY == 0)
  92.             {
  93.                 facingDirection = "w";
  94.             }
  95.             else if(faceDirectionHoldX == 1 && faceDirectionHoldY == 0)
  96.             {
  97.                 facingDirection = "e";
  98.             }
  99.             else if(faceDirectionHoldX == -1 && faceDirectionHoldY == 1)
  100.             {
  101.                 facingDirection = "nw";
  102.             }
  103.             else if(faceDirectionHoldX == 1 && faceDirectionHoldY == 1)
  104.             {
  105.                 facingDirection = "ne";
  106.             }
  107.             else if(faceDirectionHoldX == -1 && faceDirectionHoldY == -1)
  108.             {
  109.                 facingDirection = "sw";
  110.             }
  111.             else if(faceDirectionHoldX == 1 && faceDirectionHoldY == -1)
  112.             {
  113.                 facingDirection = "se";
  114.             }
  115.  
  116.             // Play animation using facingDirection string to point
  117.             // to the name of animations in the Animator Controller.
  118.  
  119.             anim.Play (facingDirection);
  120.  
  121.             #endregion
  122.         }
  123.  
  124.         else
  125.         {
  126.             #region > Play Idle Animation
  127.  
  128.             if(facingDirection != "null")
  129.             {
  130.                 anim.Play ("idle " + facingDirection);
  131.             }
  132.  
  133.             #endregion
  134.         }
  135.  
  136.         #endregion
  137.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement