Character doesn't stop and sits on top of target.

Hello there, I am currently trying to make it so that when you click on an enemy the character walks towards the target and stops at a certain range. The problem is that no matter what I set the distance to in the if statement it always walks right up to the target and sits on top of it. It fine for the player to be able to walk through/past them normally so I don’t want a collider on them or the player. Could anyone help please. I use the same code for normal walking and that will stop before if i set the distance to a larger variable.

Also I must double click off of the target to get it to move away normally which is annoying me also if anyone could hep with that.

This is the clickToMove script.

		if(Input.GetKeyDown(KeyCode.Mouse1))
		{  
			enemyTarget = null;
			Targeting targeting = GetComponent<Targeting>();

			Plane playerPlane = new Plane(Vector3.forward, transform.position);
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			float hitdist = 0.0f;

			if (targeting.selectedTarget != null) {
				enemyTarget = GameObject.FindGameObjectWithTag ("Target").transform;
				enemyDistance = Vector2.Distance(enemyTarget.position, transform.position);
			}
	
			else if(enemyTarget == null && playerPlane.Raycast (ray, out hitdist)) {
				clickPosition = ray.GetPoint(hitdist);
			}
			
		}
		if(enemyTarget != null && enemyDistance > 1.3f){ // Prevents code running when it doesn't need to
			transform.position += (enemyTarget.position - transform.position).normalized * moveSpeed * Time.deltaTime;
		}
		
		if(enemyTarget == null && playerDistance > 0.1f){ // Prevents code running when it doesn't need to
			transform.position += (clickPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
		}

This is the Targeting script.

public class Targeting : MonoBehaviour {
	
	public Transform selectedTarget = null;
	public int targetfound = 0;
	
	void Update(){

		if (Input.GetMouseButtonDown(1)){ // when button clicked...
			Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
			//Debug.Log (pos);
			RaycastHit2D hit; // cast a ray from mouse pointer:
			hit = Physics2D.Raycast (pos, Vector3.zero);

			if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
				DeselectTarget(); // deselect previous target (if any)...
				selectedTarget = hit.transform; // set the new one...
				selectedTarget.tag = "Target";
				targetfound = 1;
				Debug.Log ("Enemy Targeted");
			}
			else{
				DeselectTarget();
			}
		}
	}

	private void DeselectTarget(){
		if (selectedTarget != null){ // if any guy selected, deselect it
			selectedTarget.tag = "Enemy";
			selectedTarget = null;
		}
	}
}

Your infrastructure sure is pretty messy. For instance you have your Targeting class change the tag of your enemy and then have the movement script look for the tag instead of simply taking the selectedTarget directly from Targeting. But I don’t think that this will cause the problem. (But may cause other problems at other places).

The main problem is likely to be caused by the fact that both scripts are dependant of each other but manage input independantly. This way you have no controll about which script gets managed first. Out of the blue: I assume the movement script gets handled first, finds that targeting.selectedTarget is null and thus just move as if no target was selected and sets movement type to “no target found”. Later that frame the Targeting script gets served, detects the enemy and sets its selectTarget field accordingly. But that was just to late for the mover script.

In such cases always add in debug statements inside of each if option to check out which branch you are running into.

To fix your current problem I’d suggest to change the update method from the targeting script to an arbitrary public method and then call it from the mover script:

Inside of targeting:

public void FindTarget(){
    //The code which was inside of update but without the if Mouse
}

Inside of the mover script:

if(mouse){
    //other code
    targeting.FindTarget();
    //more code
}