void Awake() { sc = GetComponent(typeof(StepController)) as StepController; sc.character = characters[0]; clones = new List <GameObject>(); SmoothFollowCamera2 sf = cameraController.GetComponent(typeof(SmoothFollowCamera2)) as SmoothFollowCamera2; sf.target = characters[0]; characterBounds = new Vector3[characters.Length]; }
// Update is called once per frame void Update() { if (Input.GetKeyDown("return")) { switchCharacter = true; activeCharacter++; } if (Input.GetKeyDown(KeyCode.Backspace)) { showOptions = !showOptions; } if (autoSwitchCharacter) { autoSwitchCharacterTimer -= Time.deltaTime; if (autoSwitchCharacterTimer <= 0) { autoSwitchCharacterTimer = autoSwitchCharacterTime; activeCharacter++; switchCharacter = true; } autoSwitchCameraTimer -= Time.deltaTime; if (autoSwitchCameraTimer <= 0) { autoSwitchCameraTimer = autoSwitchCameraTime; SmoothFollowCamera2 sf = cameraController.GetComponent(typeof(SmoothFollowCamera2)) as SmoothFollowCamera2; sf.cameraRotSide = UnityEngine.Random.Range(-90, 90); sf.cameraRotUp = Mathf.Pow(UnityEngine.Random.value, 2) * 50; } } if (switchCharacter) { UseCharacter(activeCharacter % characters.Length); } switchCharacter = false; oldCharacter = activeCharacter; float interDist = 2.5f * Mathf.Max( Mathf.Max(characterBounds[activeCharacter].x, characterBounds[activeCharacter].y * 0.75f), characterBounds[activeCharacter].z ); if (Input.GetKeyDown(KeyCode.C)) { showClones++; } if (Input.GetKeyDown(KeyCode.X)) { showClones = Mathf.Max(0, showClones - 1); } if (showClones > clones.Count - 1 && lastCloneTime < Time.time - 1) { lastCloneTime = Time.time; Debug.Log("Adding Clone"); // Add clone for character GameObject clone = (GameObject)Instantiate(characters[activeCharacter]); clones.Add(clone); // Remove all unneccasary components from clone Component[] components; components = clone.GetComponents(typeof(Component)); foreach (Component component in components) { Type type = component.GetType(); if ( type == typeof(PlatformCharacterController) || type == typeof(AimLookCharacterController) || type == typeof(AimLookCharacterController) || type == typeof(WanderingAICharacterController) ) { Destroy(component); } } // Move clone clone.transform.position = characters[activeCharacter].transform.position + new Vector3(-2 * interDist, 2, 0); } if (showClones < clones.Count - 1) { Debug.Log("Removing Clone"); Destroy(clones[clones.Count - 1]); clones.RemoveAt(clones.Count - 1); } // Delete clones that have fallen down for (int c = 1; c < clones.Count; c++) { if (clones[c].transform.position.y - clones[0].transform.position.y < -100) { Destroy(clones[c]); clones.RemoveAt(c); c--; } } // Move clones Vector3 avgPos = characters[activeCharacter].transform.position; if (clones.Count > 1) { // Find desired direction for each clone for (int c = 1; c < clones.Count; c++) { Vector3 groupDir = (avgPos - clones[c].transform.position).normalized; Vector3 avoidanceDir = Vector3.zero; for (int other = 0; other < clones.Count; other++) { if (c == other) { continue; } Vector3 dir = clones[c].transform.position - clones[other].transform.position; float dirMag = dir.magnitude; avoidanceDir += (dir / dirMag) * Mathf.Pow(Mathf.Max(0, interDist - dirMag), 2); } NormalCharacterMotor motor = clones[c].GetComponent(typeof(NormalCharacterMotor)) as NormalCharacterMotor; Vector3 moveDir = Util.ProjectOntoPlane( clones[c].transform.InverseTransformDirection(groupDir * 2 + avoidanceDir), Vector3.up ); float moveDirMag = moveDir.magnitude; if (moveDirMag == 0) { motor.desiredMovementDirection = Vector3.zero; } else { motor.desiredMovementDirection = Vector3.Lerp( motor.desiredMovementDirection, moveDir / moveDirMag * Mathf.InverseLerp(0.2f, 1.0f, moveDirMag), 0.5f ); } } } }
private void UseCharacter(int nr) { activeCharacter = nr; // Switch relevant characters on and off for (int c = 0; c < characters.Length; c++) { if (c == nr) { characters[nr].SetActive(true); } else { characters[c].SetActive(false); } } // Apply new character to relevant controllers SmoothFollowCamera2 sf = cameraController.GetComponent(typeof(SmoothFollowCamera2)) as SmoothFollowCamera2; sf.target = characters[activeCharacter]; if (showGhost) { sf.offset = Vector3.right * -characterBounds[activeCharacter].magnitude / 2; } else { sf.offset = Vector3.zero; } sf.distance = characterBounds[activeCharacter].magnitude * (showGhost ? 1.8f : 1.2f); sc.character = characters[activeCharacter]; // Apply position to character characters[activeCharacter].transform.position = characters[oldCharacter].transform.position + Vector3.up * (initHeights[activeCharacter] - initHeights[oldCharacter]); if (clones.Count > 0) { for (int c = 1; c < clones.Count; c++) { Destroy(clones[c]); } } clones.Clear(); clones.Add(characters[activeCharacter]); if (ghost != null) { Destroy(ghost); ghost = null; } if (showGhost) { // Add ghost for character ghost = (GameObject)Instantiate(characters[activeCharacter]); // Remove all unneccasary components from ghost Component[] components; int tries = 0; do { components = ghost.GetComponents(typeof(Component)); foreach (Component component in components) { Type type = component.GetType(); if ( type != typeof(Transform) && type != typeof(Animation) && ( (tries > 0) || ( type != typeof(LegController) && type != typeof(CharacterController) && type != typeof(AlignmentTracker) ) ) ) { Destroy(component); } } tries++; } while (components.Length > 2 && tries < 2); // Add ghost scrip to ghost GhostOriginal ghostComponent = ghost.AddComponent(typeof(GhostOriginal)) as GhostOriginal; ghostComponent.character = characters[activeCharacter]; ghostComponent.offset = new Vector3(-characterBounds[activeCharacter].magnitude, 0, 0); LegAnimator LegA = characters[activeCharacter].GetComponent(typeof(LegAnimator)) as LegAnimator; LegA.ghost = ghost; } }