Search Unity

Animation plays in blend tree but not in game

Discussion in 'Animation' started by jleven22, Feb 4, 2020.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I followed a Udemy tutorial that just takes a sprite renderer image and bounces it around for animation.

    Now I'm going back and adding my own animated sprites, but am struggling to even set up the Idle.

    The animation appears to play fine in the blend tree. I have it set up so that the direction of Idle is determined by the last direction of keys moved.

    Right now, the Player looks to a child for the sprite renderer, so I've got the animation set up on that child. The issues I'm encountering are:
    • Input doesn't appear to be setting the "lastDirX" or Y. I have tried debugging, doesn't appear to be accepting the input.
    • The sprite renderer of the child object is still static, and not being replaced by the default state, which is the idle blend tree
    Here is the code:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (canMove && !LevelManager.instance.isPaused)
    4.         {
    5.  
    6.             //Moving
    7.             moveInput.x = Input.GetAxisRaw("Horizontal");
    8.             moveInput.y = Input.GetAxisRaw("Vertical");
    9.  
    10.             moveInput.Normalize();
    11.  
    12.             //transform.position += new Vector3(moveInput.x * Time.deltaTime * moveSpeed, moveInput.y * Time.deltaTime * moveSpeed, 0f);
    13.  
    14.             theRB.velocity = moveInput * activeMoveSpeed;
    15.  
    16.             Vector3 mousePosition = Input.mousePosition;
    17.             Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);
    18.  
    19.             if (mousePosition.x < screenPoint.x)
    20.             {
    21.                 transform.localScale = new Vector3(-1f, 1f, 1f);
    22.                 gunArm.localScale = new Vector3(-1f, -1f, 1f);
    23.             }
    24.             else
    25.             {
    26.                 transform.localScale = Vector3.one;
    27.                 gunArm.localScale = Vector3.one;
    28.             }
    29.  
    30.             //rotate gun arm
    31.             Vector2 offset = new Vector2(mousePosition.x - screenPoint.x, mousePosition.y - screenPoint.y);
    32.             float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
    33.             gunArm.rotation = Quaternion.Euler(0, 0, angle);
    34.  
    35.             //Firing
    36.             /*if (Input.GetMouseButtonDown(0))
    37.             {
    38.                 Instantiate(bulletToFire, firePoint.position, firePoint.rotation);
    39.                 shotCounter = TimeBetweenShots;
    40.  
    41.                 AudioManager.instance.PlaySFX(shootSound);
    42.             }
    43.  
    44.             if (Input.GetMouseButton(0))
    45.             {
    46.                 shotCounter -= shotCounter - Time.deltaTime;
    47.  
    48.                 if (shotCounter <= 0)
    49.                 {
    50.                     Instantiate(bulletToFire, firePoint.position, firePoint.rotation);
    51.  
    52.                     shotCounter = TimeBetweenShots;
    53.                 }
    54.             }*/
    55.  
    56.             if (Input.GetKeyDown(KeyCode.Tab))
    57.             {
    58.                 if(availableGuns.Count > 0)
    59.                 {
    60.                     currentGun++;
    61.                     if(currentGun >= availableGuns.Count)
    62.                     {
    63.                         currentGun = 0;
    64.                     }
    65.  
    66.                     SwitchGun();
    67.                 }
    68.                 else
    69.                 {
    70.                     Debug.LogError("Player has no guns!");
    71.                 }
    72.             }
    73.  
    74.  
    75.             //Dashing
    76.             if (Input.GetKeyDown(KeyCode.Space))
    77.             {
    78.                 if (dashCoolCounter <= 0 && dashCounter <= 0)
    79.                 {
    80.  
    81.                     AudioManager.instance.PlaySFX(dashSound);
    82.  
    83.                     activeMoveSpeed = dashSpeed;
    84.                     dashCounter = dashLength;
    85.  
    86.                     anim.SetTrigger("dash");
    87.  
    88.                     PlayerHealthController.instance.MakeInvincible(dashInvincibility);
    89.                 }
    90.             }
    91.  
    92.  
    93.             if (dashCounter > 0)
    94.             {
    95.                 dashCounter -= Time.deltaTime;
    96.                 if (dashCounter <= 0)
    97.                 {
    98.                     activeMoveSpeed = moveSpeed;
    99.                     dashCoolCounter = dashCoolDown;
    100.                 }
    101.             }
    102.  
    103.             if (dashCoolCounter > 0)
    104.             {
    105.                 dashCoolCounter -= Time.deltaTime;
    106.             }
    107.  
    108.  
    109.             //Animation
    110.             if (moveInput != Vector2.zero)
    111.             {
    112.                 anim.SetBool("isMoving", true);
    113.                 anim.SetBool("isIdle", false);
    114.             }
    115.             else
    116.             {
    117.                 anim.SetBool("isMoving", false);
    118.                 anim.SetBool("isIdle", true);
    119.             }
    120.         }
    121.  
    122.         else
    123.         {
    124.             theRB.velocity = Vector2.zero;
    125.             anim.SetBool("isMoving", false);
    126.             anim.SetBool("isIdle", true);
    127.  
    128.  
    129.  
    130.             //Animation for idle
    131.             if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
    132.             {
    133.                 currDirX = 0f;
    134.                 currDirY = 1f;
    135.                 lastDirX = 0f;
    136.                 lastDirY = 1f;
    137.             }
    138.             if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
    139.             {
    140.                 currDirX = 0f;
    141.                 currDirY = 1f;
    142.                 lastDirX = 0f;
    143.                 lastDirY = 1f;
    144.             }
    145.             else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
    146.             {
    147.                 currDirX = -1f;
    148.                 currDirY = 1f;
    149.             }
    150.             else if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
    151.             {
    152.                 currDirX = -1f;
    153.                 currDirY = 0f;
    154.                 lastDirX = -1f;
    155.                 lastDirY = 0f;
    156.             }
    157.             else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
    158.             {
    159.                 currDirX = -1f;
    160.                 currDirY = -1f;
    161.             }
    162.             else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
    163.             {
    164.                 currDirX = 0f;
    165.                 currDirY = -1f;
    166.                 lastDirX = 0f;
    167.                 lastDirY = -1f;
    168.             }
    169.             else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
    170.             {
    171.                 currDirX = 1f;
    172.                 currDirY = -1f;
    173.             }
    174.             else if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
    175.             {
    176.                 currDirX = 1f;
    177.                 currDirY = 0f;
    178.                 lastDirX = 1f;
    179.                 lastDirY = 0f;
    180.             }
    181.             else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
    182.             {
    183.                 currDirX = 1f;
    184.                 currDirY = 1f;
    185.             }
    186.  
    187.             //Idle Direction
    188.             anim.SetFloat("LastMoveX", lastDirX);
    189.             anim.SetFloat("LastMoveY", lastDirY);
    190.         }
    191.  
    192.     }
    Sorry if the extra code is distracting, but it might contain relevant context.

    ANY HELP APPRECIATED!