so basically i got one problem with attack animation, when i’m pressing attack button while running my player is staring to slide and attack, who can suggest solution to that problem. Here is my code!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KilluaControl : MonoBehaviour
{
private Rigidbody2D rb;
private Animator anim;
public float moveSpeed;
private bool attack;
private float driX;
private bool facingRight = true;
private Vector3 localScale;
private void Start (){
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
localScale = transform.localScale;
moveSpeed = 15f;
}
private void Update ()
{
if(!this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attacking"))
{
driX = Input.GetAxisRaw("Horizontal") * moveSpeed;
if(Input.GetButtonDown("Jump") && rb.velocity.y == 0)
rb.AddForce(Vector2.up * 1000f);
if(Mathf.Abs(driX) > 0 && rb.velocity.y == 0)
anim.SetBool("isRunning", true);
else
anim.SetBool("isRunning", false);
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", false);
}
if (rb.velocity.y > 0)
anim.SetBool("isJumping", true);
if (rb.velocity.y < 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", true);
}
}
{
HanldeInput();
}
}
private void HandleAttacks()
{
if(attack && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attacking"))
{
anim.SetTrigger("isAttacking1");
}
}
private void HanldeInput()
{
if (Input.GetMouseButtonDown(0))
{
attack = true;
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(driX, rb.velocity.y);
HandleAttacks();
ResetValues();
}
private void LateUpdate()
{
if (driX > 0)
facingRight = true;
else if (driX < 0)
facingRight = false;
if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
private void ResetValues()
{
attack = false;
}
}