Search Unity

Can't get Run animation to work.

Discussion in 'Animation' started by Karinph, Sep 17, 2021.

  1. Karinph

    Karinph

    Joined:
    Aug 30, 2021
    Posts:
    1
    I am still super new to unity and coding in general so please keep that in mind!

    I'm trying to get my "Run" animation but it float... I thought it was a box collider 2D error but it's not. I don't know what's wrong. any idea?

    There isn't any error in the console so I can't figure out.

    I would really appreciate any clue

    Image #1: https://ibb.co/Fsjyc6c ("standing" animation) 1.PNG

    Image #2: https://ibb.co/VtqsnH8 (Run animation error)
    2.png


    Here is my script:

    public class PlayerMoves : MonoBehaviour
    {
    public float runSpeed = 2;

    public float jumpSpeed = 4;

    Rigidbody2D rb2D;

    // ASIGNAR MEJORA DE SALTO

    public bool betterJump = false;

    public float fallMultiplier = 0.5f;

    public float lowJumpMultiplier = 1f;

    // ASIGNAR SPRITE RENDERER PARA FLIPPEAR EL PLAYER

    public SpriteRenderer spriteRenderer;

    // ASIGNAR MOVIMIENTO DE CORRER

    public Animator animator;


    void Start()
    {
    rb2D = GetComponent<Rigidbody2D>();
    }

    // MOVIMIENTO DE PERSONAJE A LA DERECHA
    void FixedUpdate()
    {
    if (Input.GetKey("d") || Input.GetKey("right"))
    {
    rb2D.velocity = new Vector2(runSpeed, rb2D.velocity.y);
    spriteRenderer.flipX = false;
    animator.SetBool("Run", true);

    }

    // MOVIMIENTO DE PERSONAJE A LA IZQUIERDA

    else if (Input.GetKey("a") || Input.GetKey("left"))
    {
    rb2D.velocity = new Vector2(-runSpeed, rb2D.velocity.y);
    spriteRenderer.flipX = true;
    animator.SetBool("Run", true);
    }

    // REPOSO DE PERSONAJE


    else
    {
    rb2D.velocity = new Vector2(0, rb2D.velocity.y);
    animator.SetBool("Run", false);

    }

    // MOVIMIENTO DE PERSONAJE PARA SALTAR

    if (Input.GetKey("space") && (CheckGround.isGrounded))
    {
    rb2D.velocity = new Vector2(rb2D.velocity.x, jumpSpeed);
    }

    //JUMP ANIMATION
    if (CheckGround.isGrounded==false)
    {
    animator.SetBool("Jump", true);
    animator.SetBool("Run", false);
    }

    if (CheckGround.isGrounded == true)
    {
    animator.SetBool("Jump", false);
    }


    // MOVIMIENTO DE MEJORA DE SALTO

    if (betterJump)
    {
    if (rb2D.velocity.y<0)
    {
    rb2D.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier) * Time.deltaTime;
    }

    if (rb2D.velocity.y>0 && !Input.GetKey("space"))
    {
    rb2D.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier) * Time.deltaTime;
    }
    }


    }
    }
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    244
    Hey,

    When posting code, you should use the "Insert Code" button:
    Insert_Code.JPG
    This will make it much easier to read.

    There's possibly something wrong with "CheckGround." However, I think it's likely an error with your animation. Can you try replacing the run animation with something else and see if still floats:
    Run_Replace.JPG

    If it no longer floats, the issue is with the run animation itself.
     
    Karinph likes this.
unityunity