// Update is called once per frame void Update() { #if T7T_ASTAR base.Update(); #else // Check if agent has reached destination. See answers.unity3d.com 324589 if (myFormation) { if (myFormation.GetState() == FormationStates.Move) { if (!agent.pathPending) { if (agent.remainingDistance <= agent.stoppingDistance) { if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f) { myFormation.ChangeState(FormationStates.Arrive); TargetReached.Invoke(); // execute the Unity Event (attached through the inspector) } } } } } // calculate real agent speed: Vector3 curMove = transform.position - previousPosition; curSpeed = curMove / Time.deltaTime; previousPosition = transform.position; #endif }
// Update is called once per frame void Update() { time += Time.deltaTime; if (time >= period) { time = 0.0f; // check if a formation is now in range for (int i = 0; i < toolbox.allFormations.Count; i++) { FormationGrid fg = toolbox.allFormations[i]; //Debug.Log(Vector3.Distance(fg.transform.position, transform.position)); if (Vector3.Distance(fg.transform.position, transform.position) < range) { //Debug.Log("IN RANGE"); GridTypes gt = fg.GetGridType(); if (gt != newGridType) { FormationAndState fas = new FormationAndState(); fas.formation = fg; fas.oldGridType = fg.GetGridType(); formationAndStates.Add(fas); fg.ChangeGridTo(newGridType); fg.ChangeState(FormationStates.Form); // start coroutine to move in waitformove seconds StartCoroutine("WaitAndStartMoving", fg); } } } // check if any of the in range formations is now out of range if (revertOnExit) { //Debug.Log("FAS Count="+ formationAndStates.Count); for (int i = formationAndStates.Count; i > 0; i--) { FormationAndState fas = formationAndStates[i - 1]; FormationGrid fg = fas.formation; //Debug.Log(Vector3.Distance(fg.transform.position, transform.position)); if (Vector3.Distance(fg.transform.position, transform.position) > range) { //Debug.Log("OUT OF RANGE"); fg.ChangeGridTo(fas.oldGridType); fg.ChangeState(FormationStates.Form); // start coroutine to move in waitformove seconds StartCoroutine("WaitAndStartMoving", fg); formationAndStates.RemoveAt(i - 1); } } } } }
IEnumerator WaitAndStartMoving(FormationGrid fg) { yield return(new WaitForSeconds(waitForMove)); fg.ChangeState(FormationStates.Move); }