Help with waypoints

So, this is my “AI” for a Tower defense I’m working at…
I’m trying to make my units to go from one point to the next, and then to the next…
As in Waypoint_1_Start >to> Waypoint_1_End >to> target, thou my units are just moving right to the target.
Also, I’m using a raycast to define if the Unit can move, if there is another unit on front of it, it won’t just run over it(if it’s an allied unit), it will “wait” till it can move again, but it isn’t working.
Any ideas why, and how to solve this problems?

using UnityEngine;
using System.Collections;

public class NavMesh_BasicAI : MonoBehaviour {

	private int pathID;
	public Transform home;
	private Transform target;
	private Transform wayPoint_1_Start;
	private Transform wayPoint_2_Start;
	private Transform wayPoint_3_Start;
	private Transform wayPoint_1_End;
	private Transform wayPoint_2_End;
	private Transform wayPoint_3_End;
	private Transform currentTarget;
	private bool transformDefined = false;
	private bool iddle = true;
	private float distance;
	private _Set_Path path;
	public float speed;

	void Start(){
		GetComponent<NavMeshAgent> ().speed = speed;
		if(transform.tag == "Player_Side"){
			path = GameObject.FindGameObjectWithTag ("Player").GetComponent<_Set_Path>();
		}
		if(transform.tag == "Enemy_Side"){
			path = GameObject.FindGameObjectWithTag("Enemy").GetComponent<_Set_Path>();
		}
		if(path){
			pathID = path.pathID;
		}
	}

	// Update is called once per frame
	void Update () {
		if(transformDefined){
			if(currentTarget != null){
				GetComponent<NavMeshAgent>().SetDestination(currentTarget.position);
				distance = Vector3.Distance (transform.position, currentTarget.position);
			}
			if(pathID == 1){
				if (currentTarget == null && wayPoint_1_Start != null) {
					currentTarget = wayPoint_1_Start;
				}
				if (currentTarget == wayPoint_1_Start && distance < 2.0f) {
					currentTarget = wayPoint_1_End;
				}
				if(currentTarget == wayPoint_1_End && distance < 2.0f){
					currentTarget = target;
				}
			}
			if(pathID == 2 && wayPoint_2_Start != null){
				if(currentTarget == null){
					currentTarget = wayPoint_2_Start;
				}
				if(currentTarget == wayPoint_2_Start && distance < 2.0f){
					currentTarget = wayPoint_2_End;
				}
				if(currentTarget == wayPoint_2_End && distance < 2.0f){
					currentTarget = target;
				}
			}
			if(pathID == 3 && wayPoint_3_Start != null){
				if(currentTarget == null){
					currentTarget = wayPoint_3_Start;
				}
				if(currentTarget == wayPoint_3_Start && distance < 2.0f){
					currentTarget = wayPoint_2_End;
				}
				if(currentTarget == wayPoint_3_End && distance < 2.0f){
					currentTarget = target;
				}
			}
		}

		if(CanMove()){
			GetComponent<NavMeshAgent>().speed = speed;
		}
		if(!CanMove()){
			GetComponent<NavMeshAgent>().speed = 0;
		}
	}

	public void DefineTransforms(Transform homedt, Transform targettd, Transform waypoint_1_startdt, Transform waypoint_1_enddt, Transform waypoint_2_startdt, Transform waypoint_2_enddt, Transform waypoint_3_startt, Transform waypoint_3_endt, bool transdefindt){
		home = homedt;
		target = targettd;
		wayPoint_1_Start = waypoint_1_startdt;
		wayPoint_1_End = waypoint_1_enddt;
		wayPoint_2_Start = waypoint_2_startdt;
		wayPoint_2_End = waypoint_2_enddt;
		wayPoint_3_Start = waypoint_3_startt;
		wayPoint_3_End = waypoint_3_endt;
		transformDefined = transdefindt;
	}

	public bool CanMove(){
		bool canMove = true;

		Ray detector = new Ray(transform.position, transform.forward);
		RaycastHit dtchit;
		if(Physics.Raycast(detector, out dtchit, 10.0f) && dtchit.transform.tag == transform.tag){
			canMove = false;
		}
		return canMove;
	}
}

You need to improve some of your if-statements. For example:

    if(transform.tag == "Player_Side"){
         path = GameObject.FindGameObjectWithTag ("Player").GetComponent<_Set_Path>();
     }
     if(transform.tag == "Enemy_Side"){
         path = GameObject.FindGameObjectWithTag("Enemy").GetComponent<_Set_Path>();
     }

Should be

    if(transform.tag == "Player_Side"){
         path = GameObject.FindGameObjectWithTag ("Player").GetComponent<_Set_Path>();
     }
     else if(transform.tag == "Enemy_Side"){
         path = GameObject.FindGameObjectWithTag ("Enemy").GetComponent<_Set_Path>();
     } 

Reason being that if the transform.tag == “Player_Side”, it clearly does not equal “Enemy_Side”.

Although the above is trivial, it does cause bigger problems later in your code; depending on whether I’ve understood what you’re trying to do or not :wink:

For example:

            if (currentTarget == null && wayPoint_1_Start != null) {
                 currentTarget = wayPoint_1_Start;
             }
             if (currentTarget == wayPoint_1_Start && distance < 2.0f) {
                 currentTarget = wayPoint_1_End;
             }
             if(currentTarget == wayPoint_1_End && distance < 2.0f){
                 currentTarget = target;
             }

In the above code, it is possible for the condition:

currentTarget == null && wayPoint_1_Start != null

to result in some circumstances

currentTarget = target;

This is because the next if-statement uses information from the previous if-statement)