// Update is called once per frame private 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) { animator.SetBool(animationParameter, newState); } } } }
public void SetFormation(FormationGrid formationgrid) { myFormation = formationgrid; }
public void CreateNewFormation() { /* * STEP 1: Define where to place the formation and how to name it * * */ // Use a Ray / Raycast from the middle of the Scene View to place the formation and its anchor on the plane/terrain/object it hits // // http://answers.unity3d.com/questions/48979/how-can-i-find-the-world-position-of-the-center-of.html Ray worldRay = SceneView.lastActiveSceneView.camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 1.0f)); // http://josbalcaen.com/unity-editor-place-objects-by-raycast/ Vector3 newPosition; RaycastHit hitInfo; // Shoot this ray. check in a distance of 10000. if (Physics.Raycast(worldRay, out hitInfo, 10000)) { newPosition = hitInfo.point; } else { newPosition = new Vector3(0, 0, 0); } // We'll use the time to name the Formation and the Anchor float t = Time.time; // Time to string see: http://answers.unity3d.com/questions/45676/making-a-timer-0000-minutes-and-seconds.html /* * STEP 2: Create the Anchor * */ GameObject anchor = new GameObject("Anchor " + string.Format("{0:0}:{1:00}.{2:0}", Mathf.Floor(t / 60), Mathf.Floor(t) % 60, Mathf.Floor((t * 10) % 10)) ); anchor.transform.position = newPosition; // new Vector3(0, 0, 0); anchor.transform.parent = formationManager.transform; FormationAnchor fa = anchor.AddComponent <FormationAnchor>(); #if T7T_ASTAR #else // anchor.AddComponent<NavMeshAgent>(); No need to add since RequireComponent in FormationAnchor's class definition NavMeshAgent nma = anchor.GetComponent <NavMeshAgent>(); if (nma != null) { nma.radius = 0.1f; nma.height = 0.1f; nma.stoppingDistance = 1.0f; } #endif GameObject sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere1.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); sphere1.transform.SetParent(anchor.transform); sphere1.transform.localPosition = new Vector3(0f, 0f, 0f); sphere1.transform.name = "empty"; /* * STEP 3: Create the Formation and link the Anchor to it * * */ GameObject formation = new GameObject("Formation " + string.Format("{0:0}:{1:00}.{2:0}", Mathf.Floor(t / 60), Mathf.Floor(t) % 60, Mathf.Floor((t * 10) % 10)) ); formation.transform.position = newPosition; formation.transform.parent = formationManager.transform; FormationGrid fg = formation.AddComponent <FormationGrid>(); AudioSource aud = formation.AddComponent <AudioSource>(); GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere2.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); sphere2.transform.SetParent(formation.transform); sphere2.transform.localPosition = new Vector3(0f, 0f, 0f); sphere2.transform.name = "empty"; //fg.ChangeGridTo(GridTypes.Wedge9); fg.SetAnchor(anchor); }
// 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); }