Waypoint system suggestions

Hello,

i try to create a system so whenever i pass thourgh a waypoint player will automatically will face the next waypoint and if player wants to go back than this time he will aim for the previous waypoint. I wrote the following script even tough it works it doesnt dowhat i want.

After passing through first waypoint and second waypoint when i wanted to go back it reverts the controls so even tough i press backward i go forward.

You can see what happens here 2016 03 26 at 17 29 53 - YouTube

I think im making a logic error somewhere but i couldnt see so far.

Anyway here is my code, im using 2 gameobjects for waypoints now,any ideas on targeting next waypoint in a kiss ( keep it simple and stupid) would be great.

using UnityEngine;
using System.Collections;

public class Control : MonoBehaviour {


	public float speed = 6.0F;

	// those decide where is our direction and where will we look,
	//will need to find a clever targeting system for passed around many wps later
	public GameObject nextWp;
	public GameObject prevWp;

	// triggered bool avoids multiple triggering on same object
	bool triggered;
	// forwarded bool helps to see direction being faced and moved is the same
	bool forwarded;
	// x saves whether we were moving forward or backward which will be used in ontrigger conditions
	float x;


	// this is the y position value
	public float curY2;


	// wait before checking triggered false for ontrigger
	IEnumerator Wait(){
		yield return new WaitForSeconds (1);
	}


	void Start(){
		// getting value for y position and setting forwarded true
			curY2=transform.rotation.y;
			forwarded=true;

		}

	

	// Update is called once per frame
	void Update () {


		// getting xvalue fromn input than move object also setting x to 1 for on trigger condition
		if (Input.GetAxis("Horizontal")> 0)
			{
				transform.Translate(Vector3.forward * Time.deltaTime *speed );
			x = 1;

			// if cube was going -x direction than this if condition happens and
			//it swaps the direction 180degrees and also set forwarded true
			if (forwarded==false)
			{
				curY2 = curY2 - 180;
				forwarded = true;

			}


			}

		// if cube was going x direction than this if condition happens and
		//it swaps the direction 180degrees and also set forwarded false
			
		if (Input.GetAxis("Horizontal")< 0)
		{
			transform.Translate(-Vector3.forward * Time.deltaTime*speed );
			x = -1;
			if (forwarded == true) 
			{
				curY2 = curY2 + 180;
				forwarded = false;
			}
		}
			
		curY2 = this.transform.rotation.y;
	}


	void OnTriggerEnter(Collider temas)
	{
		

		if (temas.gameObject.name == "45degree" && x > 0&& triggered==false) {

			transform.LookAt(nextWp.transform.position);
			Debug.Log("contact x bigger than 0");

		}

		if (temas.gameObject.name == "45degree" && x < 0&& triggered==false) {
			
			transform.LookAt(prevWp.transform.position);
			Debug.Log("contact h smaller than 1");

		}

		triggered = true;
		StartCoroutine ("Wait");
		triggered = false;
	}



}

That Wait coroutine isn’t setup correctly. Currently your trigger is just going to be setting it true then immediately setting it false. The wait won’t effect it.

     IEnumerator Wait(){
         triggered = true;
         yield return new WaitForSeconds (1);
         triggered = false;
     }

Then just call StartCoroutine("Wait"); when you want to toggle the bool. You can lose the quotations on it by the way and write StartCoroutine(Wait()); instead. As I posted in your last question you should probably roll those two if statements under OnTriggerEnter into one still though. Giving colliders excessive amount of conditions to check is just going to slow things down.

    void OnTriggerEnter(Collider temas)
    {
        if (temas.gameObject.name == "45degree" && triggered == false)
        {
            if (x > 0)
            {
                transform.LookAt(nextWp.transform.position);
                Debug.Log("contact x bigger than 0");
            }
            else if (x < 0)
            {
                transform.LookAt(prevWp.transform.position);
                Debug.Log("contact h smaller than 1");
            }
        }
        StartCoroutine(Wait());
    }