So I have a zombie with a script that makes the zombie follow the player, but as I’m new with c# im not to sure how to make it so it will chase you within a certain range and stop chasing your when out of range.
currently all it does is follow you when close enough but when I out run it it still follows me. Thanks any help.
Pay most attention on the update method as that is what needs fixing.
Thanks and heres my script:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public float deathDistance = 0.5f;
public float distanceAway;
public Transform thisObject;
public Transform target;
private NavMeshAgent navComponent;
public float dist;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
navComponent = this.gameObject.GetComponent<NavMeshAgent>();
}
void Update()
{
dist = Vector3.Distance(target.position, transform.position);
if(dist <= 30)
{
navComponent.SetDestination (target.position);
if (dist >= 30) {
target = null;
}
}
else
{
if(target = null)
{
target = this.gameObject.GetComponent<Transform>();
}
else
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
}
}
}
Hi 
you should use a Ray.
READ THIS PART ONLY IF YOU DON’T KNOW WHAT A RAY IS!!!
It kinda draws a line from a point to a direction. You can check how close something is and use your function with those distances.
here’s an quick example, it has a big collider on the enemy and this script would go on the enemy:
``<pre> private RaycastHit hit; void OnTriggerStay(Collider other) { Vector3 direction = other.transform.position - transform.position; if (Physics.Raycast(transform.position, direction.normalized, out hit)) { if (hit.collider.gameObject == player) { if (hit.distance <= 9 && hit.distance > 2.5f) { AttackThatBetch(); } else if (hit.distance <= 2.5f) { BiteThatHoe(); } else { ImmaGoLookAtThatTreeAndStandStill(); } } } } </pre>
Hope it helped 
Your approach of using Vector3.Distance is correct. It should be quicker than using a RayCast, but a RayCast is better if you don’t want your enemy to try to chase you through walls. As a learning exercise though I’d recommend sticking to Vector3.Distance for now, and once it is working changing it to a RayCast.
Your problem is that you are setting your target to null, and then in the next execution of your app trying to determine a distance from the current game object to target.transform (you will probably see an error in the Console output tab).
public class Chase : MonoBehaviour
{
public Transform player;
void Update()
{
var direction = player.position - transform.position;
var angle = Vector3.Angle(direction, this.transform.forward);
if (angle < 180 && direction.magnitude < 10)
{
var rotation = Quaternion.Slerp(
this.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime);
rotation.x = 0;
rotation.z = 0;
transform.rotation = rotation;
if (direction.magnitude > 5)
transform.Translate(0, 0, Time.deltaTime * 1.5f);
}
}
}
This code is more or less from this tutorial Basic Artificial Intelligence for a Non-Player Character with Unity 5 - YouTube - It takes you through the logic of each step, and adds animation too.