How to stop enemy within certain distance of player?

Hey I’m pretty new to Unity 3D & C sharp scripting. I was wondering how to stop the enemy from jittering against the player when within attacking distance (IE the enemy gets within attacking distance and stops 1 unit away) and also how do Istop the enemy from randomly moving up the screen when I press play. Here is my current script for the enemy ai

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
	public Transform target;
	public int moveSpeed;
	public int rotationSpeed;
	public int maxdistance;
	public float attackTime;
	public float coolDown;
	private Transform myTransform;

	void Awake()
	{ 
		myTransform = transform;
	}
	

	void Start () {
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		target = go.transform;
		attackTime = 0;
		coolDown = 4.0f;
		}

	void Update () {

	
		Debug.DrawLine(target.position, myTransform.position, Color.red); 
		if (Vector3.Distance(target.position, transform.position) < 20)
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

		{
		myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
		}
			
		if(attackTime > 0)
	    attackTime -= Time.deltaTime;
		
		if(attackTime < 0)
		attackTime = 0;
		
		if(attackTime == 0) {
		Attack();
		attackTime = coolDown;
		}
	}		
		private void Attack() {
		float distance = Vector3.Distance(target.transform.position, transform.position);
		Vector3 dir = (target.transform.position - transform.position).normalized;
		float direction = Vector3.Dot(dir, transform.forward);
		
		if(distance < 7) {
		if(direction > 0) { 
		PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
		eh.AddjustCurrentHealth(-10);
			}
		}
	}
}

Any help or hints will be greatly appreciated. Cheers :slight_smile:

I’d suggest doing a check to stop movement after it reaches its location. That way you only move when you need to move.

Something along the lines of:

if (target.transform.position != player.transform.position){
Move();
}

The if in Update seems wrong: it only controls the enemy rotation, turning to the player when closer than 20m - the enemy keeps going to the player no matter how far it is. Is it an intended feature? If not, you could change it and include the min distance this way:

void Update () {
    Debug.DrawLine(target.position, myTransform.position, Color.red); 
    float dist = Vector3.Distance(target.position, transform.position);
    if (dist < 20){ 
        // get the target direction:
        Vector3 targetDir = target.position - myTransform.position;
        targetDir.y = 0; // kill any height difference to avoid tilting
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targetDir), rotationSpeed * Time.deltaTime);
        if (dist > 2){ // check min distance
            // only move to the target if farther than min distance
            myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        }
    }
    if(attackTime > 0){
        attackTime -= Time.deltaTime;
        if (attackTime <= 0){
            Attack();
            attackTime = coolDown;
        }
    }
}

private void Attack() {
    float distance = Vector3.Distance(target.transform.position, transform.position);
    Vector3 dir = (target.transform.position - transform.position).normalized;
    float direction = Vector3.Dot(dir, transform.forward);
    if (distance < 7 && direction > 0){
       // the generic GetComponent is faster!
       PlayerHealth eh = target.GetComponent< PlayerHealth>();
       eh.AddjustCurrentHealth(-10);
    }
}