- Home /
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 https://youtu.be/tCOx3TxSQcs
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;
}
}
Answer by Fredex8 · Mar 26, 2016 at 09:36 PM
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());
}
Well so far, that script is good for stopping multiple entrances, so it shouldnt be a problem thanks for insight tough.
Sorry I hadn't watched the video to see the issue and just got distracted by optimising stuff ins$$anonymous$$d....
Your best bet for creating a waypoint system like this (without using a navmesh) is probably to create a list or array of all the waypoints. Rather than using the wait coroutine to exclude further collisions with that object you should have a variable store the current waypoint.
void OnTriggerEnter(Collider temas)
{
if (temas.gameObject.tag== "Waypoint")
{
if (currentWaypoint != temas.gameObject)
{
currentWaypoint = temas.gameObject;
//$$anonymous$$ove to next/last waypoint
}
}
}
Something like that will stop you having to worry about multiple collisions with the same waypoint. The waypoint before and after that one would be the entry before and after it in the list so you can then get these objects and navigate towards them.
Your answer
![](https://koobas.hobune.stream/wayback/20220612073400im_/https://answers.unity.com/themes/thub/images/avi.jpg)