check path and waitforseconds. countdown keeps counting down
Heyhey!, so iam working on this game: https://dribbble.com/shots/2119802-iMaze
pretty easy thing, you get a generated maze and have to solve it in a certain time. for this i am using a navmesh on the ground. navmesh obstacles on the maze tile borders and a navmesh agent on the red ball. and every time you rotate a mesh, the "StartCoroutine(CheckPath());" gets called.
the navcheck script looks like this at the moment:
using UnityEngine;
using System.Collections;
using plyBloxKit;
using qtools.qmaze;
using qtools.qmaze.example;
public class NavMeshCheck : MonoBehaviour
{
public Transform target;
public GameObject Ampel_Green;
public GameObject Ampel_Red;
public GameObject Attention;
public bool Path;
private NavMeshAgent agent;
private GameObject rotatingPiece = null;
void Awake()
{
Ampel_Red.SetActive(true);
Ampel_Green.SetActive(false);
Attention.SetActive(false);
//for (int i = 0; i < pieceList.Count; i++)
//{
//QMazePieceRotator piece = pieceList[ i ];
//piece.onPieceRotated += OnPieceRotated;
//}
}
public void OnNewMazePieceGenerated( QMazePieceData piece )
{
Debug.Log( "New piece generated: " + piece.geometry.name );
var rotator = piece.geometry.GetComponent<QMazePieceRotator>();
// Register as lister of the pieces that can rotate
if( rotator != null )
{
rotator.onPieceRotationBegin += OnPieceRotationBegin;
rotator.onPieceRotationEnd += OnPieceRotationEnd;
}
}
void OnPieceRotationBegin( GameObject piece )
{
rotatingPiece = piece;
}
void OnPieceRotationEnd( GameObject piece )
{
//CheckPath();
StartCoroutine(CheckPath());
return;
if( rotatingPiece == piece )
{
rotatingPiece = null;
//CheckPath();
StartCoroutine(CheckPath());
}
}
//void CheckPath()
IEnumerator CheckPath()
{
agent = GetComponent<NavMeshAgent>();
NavMeshPath path = new NavMeshPath();
agent.CalculatePath(target.position, path);
//GetComponent<Animation>().Play("Ball_Jump");
if (path.status == NavMeshPathStatus.PathComplete)
{
yield return new WaitForSeconds(3f);
Path =true;
Ampel_Green.SetActive(true);
Ampel_Red.SetActive(false);
Attention.SetActive(true);
plyBloxGlobal.Instance.SetVarValue("Puzzle", false);
Debug.Log("PathComplete");
}
else
{
Path =false;
//Ampel_Red.SetActive(true);
//Ampel_Green.SetActive(false);
//plyBloxGlobal.Instance.SetVarValue("Puzzle", true);
Attention.SetActive(false);
Debug.Log("PathInvalid");
}
}
}
and everything works fine so far, but i have this "yield return new WaitForSeconds(3f);" cause i want to give the player a short delay before the maze gets marked as solved (Path =true;).
but the problem i am facing now is, when this happens: the maze gets solved (Path =true;) and during the three seconds countdown you change your mind and rotate another peace which leads to the situation that the maze is not solved. so the path from ball to exit is not true. But for some reason the countdown is still counting down in this case and sets the path to true. But it should be resetted
Your answer
Follow this Question
Related Questions
Checking if a 2D joint gets pulled or pushed 0 Answers
Move a object around another object 0 Answers
Need help with an image path script 0 Answers
Issue with waypoint tiles 0 Answers
Waiting for time for enemy to attack 1 Answer