Search Unity

Question Transition from idle to running animation won't work

Discussion in 'Animation' started by Fedef3sso_, Oct 11, 2020.

  1. Fedef3sso_

    Fedef3sso_

    Joined:
    Oct 11, 2020
    Posts:
    3
    I've been trying all day to get the animation for my 2D character working but, no matter what I do, it's stuck on its idle animation. I made an animation script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAnimation : MonoBehaviour
    6. {
    7.  
    8.     private Animator anim;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
    20.         {
    21.             anim.SetBool("isRunning", true);
    22.         }
    23.         else
    24.         {
    25.             anim.SetBool("isRunning", false);
    26.         }
    27.     }
    28. }
    Set the bool parameter "isRunning" in the animator, made the transition with the "isRunning" parameter and dragged the animator script on the Player in the inspector. Can anyone help me?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    Can you show what the transitions you have made in the Animator?
     
  3. Fedef3sso_

    Fedef3sso_

    Joined:
    Oct 11, 2020
    Posts:
    3
    I managed to fix it, but thanks for replying!
    I have a new issue now, though.
    I'm trying to:
    - Jump as long as the Space key is pressed and playing the corresponding jump up animation;
    - Start falling when the Space key is released and play the fall animation;
    - Transition from the fall animation to run animation if the player is grounded AND there's an active A or D input;
    - Transition from fall to idle if the player is grounded without any input.

    That's a lot to ask, but I would really appreciate it if you could help me!

    Here's the player movement script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float jumpForce;
    9.     private float moveInput;
    10.  
    11.     private Rigidbody2D rb2d;
    12.  
    13.     private bool facingRight = true;
    14.  
    15.     public bool isGrounded;
    16.     public Transform groundCheck;
    17.     public float checkRadius;
    18.     public LayerMask whatIsGround;
    19.  
    20.     private int extraJumps;
    21.     public int extraJumpsValue;
    22.  
    23.     private void Start()
    24.     {
    25.         extraJumps = extraJumpsValue;
    26.         rb2d = GetComponent<Rigidbody2D>();
    27.     }
    28.  
    29.     private void FixedUpdate()
    30.     {
    31.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    32.         moveInput = Input.GetAxis("Horizontal");
    33.         rb2d.velocity = new Vector2(moveInput * speed, rb2d.velocity.y);
    34.  
    35.         if(facingRight == false && moveInput > 0)
    36.         {
    37.             Flip();
    38.         }
    39.         else if(facingRight == true && moveInput < 0)
    40.         {
    41.             Flip();
    42.         }
    43.     }
    44.  
    45.     private void Update()
    46.     {
    47.         if(isGrounded == true)
    48.         {
    49.             extraJumps = extraJumpsValue;
    50.         }
    51.  
    52.         if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    53.         {
    54.             rb2d.velocity = Vector2.up * jumpForce;
    55.             extraJumps--;
    56.         }
    57.         else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
    58.         {
    59.             rb2d.velocity = Vector2.up * jumpForce;
    60.         }
    61.     }
    62.  
    63.     void Flip()
    64.     {
    65.         facingRight = !facingRight;
    66.         Vector3 Scaler = transform.localScale;
    67.         Scaler.x *= -1;
    68.         transform.localScale = Scaler;
    69.     }
    70. }
    Player animation script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAnimation : MonoBehaviour
    6. {
    7.  
    8.     private Animator anim;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    20.         {
    21.             anim.SetBool("isRunning", true);
    22.         }
    23.         else
    24.         {
    25.             anim.SetBool("isRunning", false);
    26.         }
    27.  
    28.         if(Input.GetKeyDown(KeyCode.Space))
    29.         {
    30.             anim.SetTrigger("jump");
    31.         }
    32.         else if(Input.GetKeyUp(KeyCode.Space))
    33.         {
    34.             anim.SetTrigger("fall");
    35.         }
    36.  
    37.         if(GetComponent<PlayerController>().isGrounded == true && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    38.         {
    39.             anim.SetBool("isRunning", true);
    40.         }
    41.         else
    42.         {
    43.             anim.SetBool("isRunning", false);
    44.         }
    45.     }
    46. }
    Screenshots of the animator:

    upload_2020-10-13_11-34-9.png upload_2020-10-13_11-34-14.png upload_2020-10-13_11-34-21.png upload_2020-10-13_11-34-25.png upload_2020-10-13_11-34-30.png
     
  4. lelegio06

    lelegio06

    Joined:
    Nov 22, 2020
    Posts:
    1
    the controller work but the animation don't work (for me)