Animation will not stop playing

Ok so im working on a script for basic attacking animation and for whatever reason the animation repeats even though i added a boolean to change it to false. i cant figure out whats going wrong at this pint

using UnityEngine;
using System.Collections;

public class Player1 : Creature
{

    public static Player1 player;

    public static bool isAttacking;

    public static Transform opponent;
    Animation animation;
    public AnimationClip attackAnimation;

    // Use this for initialization
    void Awake()
    {
        player = this;
        animation = GetComponent<Animation>();
        isAttacking = false;
    }

    // Update is called once per frame
    void Update()
    {
        Attack();
    }

    protected override void Attack()
    {
        if (Input.GetKeyUp(KeyCode.X))
        {
            if (opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
            {
                isAttacking = true;
                animation.CrossFade(attackAnimation.name);
                // opponent.GetComponent<Enemy>().GetHit(damage);
            }
        }
        if (!animation.IsPlaying(attackAnimation.name))
        {
            isAttacking = false;
        }

    }
}

Please use the Animator as you can do this within it.
They added the Animator window and component ages ago to make exactly what you are doing easier.
It has booleans, triggers, floats, ints. you name it. Give it a try.

i normally use the animator but its for a specific project

Im sorry but I cannot think of a reason for this. “for a specific project”? Care to elaborate? Does it mean you are not allowed to use the Animator or just dont want to?

If you’re not going to use the animator, you will need an entire state system in your code. I don’t see anything that handles what state your “character” is in.

So for example, in this line:

if (opponent != null && Vector3.Distance(opponent.position, transform.position) < range)

You’re not checking to make sure you’re not already attacking before setting the animation, so it will set every frame that this is true. Even though KeyUp is one event, it may trigger multiple times.

if (opponent != null && Vector3.Distance(opponent.position, transform.position && !isAttacking) < range)

This will only fire when “isAttacking” is false.

You will need a state for idle, a state for whatever else. You have to manually switch it back if you’re bypassing the animator.

I even went one step further and divided the time and manually switched frames. That was a long process.