Detecting player location incorrectly?

#pragma strict

var target : Transform;
var anim : Animator;
var nav : NavMeshAgent;

function Start()
{
    anim = GetComponent("Animator");
    nav = this.gameObject.GetComponent("NavMeshAgent");
    target = GameObject.FindGameObjectWithTag("Player").transform;
}

function Update()
{
    if(target)
    {
        nav.SetDestination(target.position);
    }
    else
    {
        if(target == null)
        {
            target = this.gameObject.GetComponent(Transform);
        }
        else
        {
            target = GameObject.FindGameObjectWithTag("Player").transform;
        }
    }

    var distance = Vector3.Distance(this.gameObject.transform.position, target.transform.position);

    if (distance <= 20)
    {
        transform.Translate(Vector3.forward);
        anim.SetFloat ("Speed", 2f);
        anim.SetBool("Running", true);
        anim.SetBool("Walking", false);
    }
    else if (distance <= 999 && distance > 20)
    {
        transform.Translate(Vector3.forward);
        anim.SetFloat ("Speed", 1f);
        anim.SetBool("Running", false);
        anim.SetBool("Walking", true);
    }
}

When I run this, the enemy that should be chasing me doesn't chase me, he just moves really fast in random directions. Can anyone tell me how to make this script more effective, and actually make him move towards me, changing animations depending on the condition? Thanks

Why are you translating the character as wellas using a NavMeshAgent?

Remove lines 36 & 43, they are unnecessary. As you are using SetDestination, which will make the agent traverse automatically.

transform.Translate(Vector3.forward);

That should fix your issues aslong as target.position is as you expect.
Additionally to reduce the performance overhead. I'd consider reducing the rate at which you call SetDestination. Calling it every frame is overkill for most situations.