Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Troubles with animations and blend trees

Discussion in 'Animation' started by LazyGoblinCody, Aug 17, 2017.

  1. LazyGoblinCody

    LazyGoblinCody

    Joined:
    Mar 18, 2015
    Posts:
    66
    So right now I have a walking animation and I have a blend tree set up for walking up, down, left and to the right. Problem is that when the key is let go it seems the animation gets defaulted to the first animation on the blend tree list.

    When exit time is turned off this does not seem to be the problem the only thing is is that with exit time off the and if the player only moves one tile over no part of the animation will play so it will seem as if though the player is sliding.

    Here is my movement code and I also posted a gif below of what the animation looks like now as well as apicture of the blend tree it self.

    Code (CSharp):
    1.   public enum Direction
    2.     {
    3.         Up,
    4.         Down,
    5.         Left,
    6.         Right
    7.     }
    8.  
    9.     Direction currentDir = Direction.Down;
    10.     public bool canMove = true, moving = false;
    11.     public float walkSpeed, runSpeed;
    12.     public int buttonCooldown = 0;
    13.     private Vector3 pos;
    14.     public float gridLength;
    15.     public bool moved;
    16.     public bool isWalking;
    17.     public bool isRunning;
    18.     public GameObject tile;
    19.     Animator anim;
    20.     Animator playerAnim;
    21.  
    22.     public Vector2 lastMove;
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.         tile = this.gameObject.transform.GetChild(0).gameObject;
    27.         anim = tile.GetComponent<Animator>();
    28.         playerAnim = GetComponent<Animator>();
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         RunWalkSwitch();
    34.         playerAnim.SetBool("PlayerMoving", false);
    35.         playerAnim.SetBool("PlayerRunning", false);
    36.  
    37.         if ((Input.GetAxisRaw("Horizontal") > 0.9f || Input.GetAxisRaw("Horizontal") < -0.9f) && isWalking)
    38.         {
    39.             playerAnim.SetBool("PlayerMoving", true);
    40.             playerAnim.SetBool("PlayerRunning", false);
    41.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    42.  
    43.         }else if ((Input.GetAxisRaw("Vertical") > 0.9f || Input.GetAxisRaw("Vertical") < -0.9f) && isWalking)
    44.         {
    45.             playerAnim.SetBool("PlayerMoving", true);
    46.             playerAnim.SetBool("PlayerRunning", false);
    47.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    48.  
    49.         }else if ((Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) && isRunning)
    50.         {
    51.             playerAnim.SetBool("PlayerRunning", true);
    52.             playerAnim.SetBool("PlayerMoving", false);
    53.  
    54.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    55.  
    56.         }else if ((Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) && isRunning)
    57.         {
    58.             playerAnim.SetBool("PlayerRunning", true);
    59.             playerAnim.SetBool("PlayerMoving", false);
    60.  
    61.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    62.  
    63.         }
    64.  
    65.         playerAnim.SetFloat("LastMoveX", lastMove.x);
    66.         playerAnim.SetFloat("LastMoveY", lastMove.y);
    67.  
    68.         buttonCooldown--;
    69.         moved = false;
    70.  
    71.        
    72.  
    73.         if (canMove)
    74.         {
    75.             pos = transform.position;
    76.             Movement();
    77.         }
    78.  
    79.         if (moving)
    80.         {
    81.  
    82.  
    83.             if (transform.position == pos)
    84.             {
    85.                 moving = false;
    86.                 canMove = true;
    87.                 Movement();
    88.              
    89.  
    90.             }
    91.             if (isWalking)
    92.             {
    93.                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * walkSpeed);
    94.             }else if (isRunning)
    95.             {
    96.                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * runSpeed);
    97.             }
    98.  
    99.         }
    100.        
    101.     }
    102.  
    103.     void Movement()
    104.     {
    105.         if(buttonCooldown <= 0)
    106.         {
    107.             if (Input.GetAxisRaw("Vertical") > 0.5f)
    108.             {
    109.                 if(currentDir != Direction.Up)
    110.                 {
    111.                  
    112.                     buttonCooldown = 5;
    113.                     currentDir = Direction.Up;
    114.                 }else
    115.                 {
    116.  
    117.                     canMove = false;
    118.                     moving = true;
    119.                     moved = true;
    120.                     pos += Vector3.up / gridLength;
    121.                 }
    122.              
    123.             }else if (Input.GetAxisRaw("Vertical") < -0.5f)
    124.             {
    125.                 if(currentDir != Direction.Down)
    126.                 {
    127.                  
    128.                     buttonCooldown = 5;
    129.                     currentDir = Direction.Down;
    130.                 }else
    131.                 {
    132.                     canMove = false;
    133.                     moving = true;
    134.                     moved = true;
    135.                     pos += Vector3.down / gridLength;
    136.                 }
    137.             }else if (Input.GetAxisRaw("Horizontal") < -0.5f)
    138.             {
    139.  
    140.                 if (currentDir != Direction.Left)
    141.                 {              
    142.                     buttonCooldown = 5;
    143.                     currentDir = Direction.Left;
    144.                 }else
    145.                 {
    146.                     canMove = false;
    147.                     moving = true;
    148.                     moved = true;
    149.                     pos += Vector3.left / gridLength;
    150.                 }
    151.             }
    152.             else if (Input.GetAxisRaw("Horizontal") > 0.5f)
    153.             {
    154.                 if (currentDir != Direction.Right)
    155.                 {
    156.                  
    157.                     buttonCooldown = 5;
    158.                     currentDir = Direction.Right;
    159.                 }
    160.                 else
    161.                 {
    162.                     canMove = false;
    163.                     moving = true;
    164.                     moved = true;
    165.                     pos += Vector3.right / gridLength;
    166.                 }
    167.             }
    168.  
    169.             playerAnim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    170.            
    171.             playerAnim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    172.          
    173.         }
    174.  
    175.     }
    176.  
    177.     void RunWalkSwitch()
    178.     {
    179.         if(Input.GetKeyDown(KeyCode.E) && isWalking)
    180.         {
    181.             anim.SetBool("SwitchToRun", true);
    182.             anim.SetBool("SwitchToWalk", false);
    183.  
    184.             isRunning = true;
    185.             isWalking = false;
    186.         }else if(Input.GetKeyDown(KeyCode.E) && isRunning)
    187.         {
    188.            
    189.             anim.SetBool("SwitchToWalk", true);
    190.             anim.SetBool("SwitchToRun", false);
    191.             isRunning = false;
    192.             isWalking = true;
    193.         }
    194.     }
    195.  
    196.     void EndRunTileSwitch()
    197.     {
    198.         anim.SetBool("SwitchToWalk", false);
    199.         anim.SetBool("SwitchToRun", false);
    200.     }
     

    Attached Files:

  2. LazyGoblinCody

    LazyGoblinCody

    Joined:
    Mar 18, 2015
    Posts:
    66
  3. cathat00

    cathat00

    Joined:
    Apr 7, 2018
    Posts:
    6
    Bumping this 'cause I am having the exact same issue!