I'm need help with fighting game with change animations between idle and attack

I’m making a fighting game, im newbie in Unity and idk C#.

I have 2 animations: idle and attack. I’m need to play attack when i press "F" and idle must dissapear and then return to visible.

I have code to attack animation:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Attack();
        }
    }

    void Attack()
    {
        animator.SetTrigger("Attack");
    }
}

To achieve what you described, you can modify your PlayerController script to handle the visibility of the idle animation when attacking. Here’s the updated script:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Animator animator;
    private bool isAttacking = false;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if (!isAttacking && Input.GetKeyDown(KeyCode.F))
        {
            Attack();
        }
    }

    void Attack()
    {
        isAttacking = true;
        animator.SetTrigger("Attack");
        // Disable the idle animation while attacking
        animator.SetBool("IdleVisible", false);
        // You can add a coroutine here to wait for the attack animation to finish
        // and then re-enable the idle animation
        // StartCoroutine(EnableIdleAfterAttack());
    }

    // Coroutine to enable idle animation after attack animation finishes
    // IEnumerator EnableIdleAfterAttack()
    // {
    //     yield return new WaitForSeconds(/* duration of attack animation */);
    //     animator.SetBool("IdleVisible", true);
    //     isAttacking = false;
    // }
}

In this script, a boolean variable isAttacking is introduced to keep track of whether the player is currently attacking. When the “F” key is pressed (Input.GetKeyDown(KeyCode.F)), the Attack() method is called. In this method, the Attack trigger is set on the animator, and the boolean IdleVisible (you should create this parameter in your animator controller) is set to false to hide the idle animation while attacking.

You can also use a coroutine to wait for the attack animation to finish before re-enabling the idle animation. Uncomment the StartCoroutine(EnableIdleAfterAttack()); line and implement the EnableIdleAfterAttack() coroutine if you want to use this approach. This will ensure that the idle animation is only re-enabled after the attack animation finishes.