Hi, I’m newbie in Unity.
My problem is that the jump animation doesn’t stop playing until a second later when I stop moving the character, if I jump and then continuously move my character he will remain in the jump animation(Which is just one frame long) even after touching the ground until I stop moving him. If I just jump without moving back and forth, it will correctly transition to the next animation(idle animation). In every other situation everything works well, like running and then dying after hitting an enemy.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movimiento : MonoBehaviour
{
private Rigidbody2D rb2D;
private BoxCollider2D boxCollider2D;
[SerializeField] private LayerMask layerMask;
float moveSpeed = 40;
float jumpForce = 17f;
float horizontalMove;
public Animator animator;
private bool facingRight;
private int health = 3;
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
boxCollider2D = transform.GetComponent<BoxCollider2D>();
}
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal");
animator.SetFloat("speed", Mathf.Abs(horizontalMove));
if (IsGrounded() && Input.GetKeyDown(KeyCode.UpArrow))
{
rb2D.velocity = Vector2.up * jumpForce;
animator.SetBool("IsJumping", true);
}
if(rb2D.velocity.y == 0)
{
animator.SetBool("IsJumping", false);
}
}
private void FixedUpdate()
{
if(horizontalMove > 0.1f || horizontalMove < -0.1f)
{
rb2D.AddForce(new Vector2(horizontalMove * moveSpeed, 0f), ForceMode2D.Force);
}
if(horizontalMove<0 && facingRight && IsGrounded())
{
Flip();
}
else if(horizontalMove>0 && !facingRight && IsGrounded())
{
Flip();
}
}
private bool IsGrounded()
{
RaycastHit2D raycastHit2D = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, .1f, layerMask);
return raycastHit2D.collider != null;
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
public void byeBye()
{
Destroy(this);
}
[IMG]https://files.fm/thumb_show.php?i=xum73rrwu[/IMG]
