// Splitting the centipede in two relative the deadSectionIndex public void Split(int deadSectionIndex) { StopAllCoroutines(); Transform[] firstTail = tail.Take(deadSectionIndex).ToArray(); Transform[] secondTail = tail.Skip(deadSectionIndex).ToArray(); tail = firstTail; _sectionCount = tail.Length; RemoveExcessPoint(); StartCoroutine(MoveCentipede()); if (secondTail.Length > 0) { CentipedeController cc = secondTail.First().gameObject.AddComponent <CentipedeController>(); cc.SetDirection(_direction); Transform[] tmpArray = new Transform[secondTail.Length]; Array.Copy(secondTail, tmpArray, secondTail.Length); List <Transform> list = tmpArray.ToList(); list.RemoveAt(0); cc.SetTail(list.ToArray()); cc.SetUpdateTime(positionUpdateTime - positionUpdateTime * timeCoefficient); for (int i = secondTail.Length - 1; i >= 0; i--) { cc.AddNewWayPoint(secondTail[i].position); } cc.AddNewWayPoint(secondTail.First().position); } }
// Method for creating new centipede with defined body length and speed public void CreateNewCentipede(int sectionCount, float positionUpdateTime) { List <Transform> sections = new List <Transform>(); Vector3 size = GlobalVariables.CELL_SIZE * 0.35f; // Creating tail of the new centipede for (int i = 0; i < sectionCount - 1; i++) { GameObject go = Instantiate (_sectionPrefab, _spawnTransform.position, Quaternion.identity, _parentTransform); sections.Add(go.transform); go.transform.localScale = size; } // Creating the head of centipede // Setting direction, tail and speed (time btw changing position) GameObject head = Instantiate (_sectionPrefab, _spawnTransform.position, Quaternion.identity, _parentTransform); head.transform.localScale = size; CentipedeController cc = head.AddComponent <CentipedeController>(); cc.SetDirection(Vector2.right); cc.SetTail(sections.ToArray()); cc.SetUpdateTime(positionUpdateTime); }