How do I make an enemy chase the player with a C script?

Basically I’ve been searching this for ages and the techniques and scripts I’ve seen and tried haven’t been able to work, so this is why I’m asking here.

How do I write a script so that when the player walks within a certain range, the enemy will start to chase them but when they’re further away, they will just look in their direction?

Again I’ve been through the answers, I’ve googled this topic and read through a lot of forums and I still can’t seem to get it to work. This is one of the scripts that I tried to use from a tutorial(that didn’t work) so maybe someone can see where I’ve gone wrong?

using UnityEngine;
using System.Collections;

public class FogMan : MonoBehaviour {
var target : Transform; //the enemy’s target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy acces/performance
}

function Start()
{
	Target = GameObject.FindWithTag("Basehuman01b").transform; //target the player

}

function Update () {
	//rotate to look at the player
	var distance = Vector3.Distance(myTransform.position, target.position);
	if (distance<=range2 && distance>=range){
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
			Quaternion.LookRotation(target.position -- myTransform.position), rotationSpeed*Time.deltaTime);
	}

	else if(distance<=range && distance>stop){

		//move towards the player
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
			Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
		myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
	}
	else if (distance<=stop) {
		myTransform.rotation = Quaternion/Slerp(myTransform.rotation,
			Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
	}

}

I’m just going to approach this problem from scratch and try to meet the specs in as little code as possible. You ask for an enemy that will follow a player from a certain distance, and will always look at it.

C#:

//The target player
public Transform player;
//At what distance will the enemy walk towards the player?
public float walkingDistance = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public float smoothTime = 10.0f;
//Vector3 used to store the velocity of the enemy
private Vector3 smoothVelocity = Vector3.zero;

//Call every frame
void Update()
{
    //Look at the player
    transform.LookAt(player);
    //Calculate distance between player
    float distance = Vector3.Distance(transform.position, player.position);
    //If the distance is smaller than the walkingDistance
    if(distance < walkingDistance)
    {
        //Move the enemy towards the player with smoothdamp
        transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
    }
}

Javascript:

//The target player
public var player : Transform;
//At what distance will the enemy walk towards the player?
public var walkingDistance : float = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public var smoothTime : float = 10.0f;
//Vector3 used to store the velocity of the enemy
private var smoothVelocity : Vector3 = Vector3.zero;

//Call every frame
function Update()
{
    //Look at the player
    transform.LookAt(player);
    //Calculate distance between player
    distance = Vector3.Distance(transform.position, player.position);
    //If the distance is smaller than the walkingDistance
    if(distance < walkingDistance)
    {
        //Move the enemy towards the player with smoothdamp
        transform.position = Vector3.SmoothDamp(transform.position, player.position, smoothVelocity, smoothTime);
    }
}

@CraftyMaelyss Does your enemy have a collider and rigid body attached? that could be why its passing through objects.

how do i make it faster?