private void Update() { RectTransform rt = Planet.GetComponent <RectTransform>(); Vector2 mousePosition; // convert mouse location to the transforms coordinates, saves it to mousePosition RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, Input.mousePosition, Camera.main, out mousePosition); // manually get bounds of transform float width = RectTransformUtility.CalculateRelativeRectTransformBounds(rt).extents.x; float height = RectTransformUtility.CalculateRelativeRectTransformBounds(rt).extents.y; // if in bounds of a planet if (Mathf.Abs(mousePosition.x) <= width && Mathf.Abs(mousePosition.y) <= height) { if (CountdownTimer.IsRunning()) { CountdownTimer.IncrementTime(Time.deltaTime); } else { CountdownTimer.Start(); CountdownTextbox.text = "" + Countdown; Countdown1.Play(); } if (CountdownTimer.TimerFinished()) { Countdown--; CountdownTextbox.text = "" + Countdown; if (Countdown == 0) { CountdownTextbox.text = ""; Countdown2.Play(); SceneManager.LoadScene(0); // level select } else { Countdown1.Play(); } } } // else the mouse is not on the current planet else { CountdownTimer.Stop(); Countdown = 3; CountdownTextbox.text = ""; } // not really sure why these didn't work, something was happening with the Bounds.Contains(Vector3) method //Debug.Log(RectTransformUtility.CalculateRelativeRectTransformBounds(rt).Contains(new Vector3(mousePosition.x, mousePosition.y, 0f))); //Debug.Log(RectTransformUtility.CalculateRelativeRectTransformBounds(rt).Contains(mousePosition)); }
private void Update() { for (int i = 0; i < Planets.Length; i++) { RectTransform rt = Planets[i].GetComponent <RectTransform>(); Vector2 mousePosition; // convert mouse location to the transforms coordinates, saves it to mousePosition RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, Input.mousePosition, Camera.main, out mousePosition); // manually get bounds of transform float width = RectTransformUtility.CalculateRelativeRectTransformBounds(rt).extents.x; float height = RectTransformUtility.CalculateRelativeRectTransformBounds(rt).extents.y; // if in bounds of a planet if (Mathf.Abs(mousePosition.x) <= width && Mathf.Abs(mousePosition.y) <= height) { if (SelectedLevel == 0) { SelectedLevel = i + 1; // need to count level select scene } } // else the mouse is not on the current planet else { // make sure this was not supposed to be the selected planet. // if it was, deselect and reset countdown. if (i == SelectedLevel - 1) { SelectedLevel = 0; CountdownTextboxes[i].text = ""; CountdownTimer.Stop(); Countdown = 3; } } // not really sure why these didn't work, something was happening with the Bounds.Contains(Vector3) method //Debug.Log(RectTransformUtility.CalculateRelativeRectTransformBounds(rt).Contains(new Vector3(mousePosition.x, mousePosition.y, 0f))); //Debug.Log(RectTransformUtility.CalculateRelativeRectTransformBounds(rt).Contains(mousePosition)); } // if a planet is selected, check for passage of time // otherwise, reset the countdown and stop the timer if (SelectedLevel != 0) { // updates the time if it is running // otherwise, starts a timer and countdown if (CountdownTimer.IsRunning()) { CountdownTimer.IncrementTime(Time.deltaTime); } else { /* * // old method of updating labels, was excessive * TMP_Text[] textboxes = Planets[i].GetComponentsInChildren<TMP_Text>(); * foreach (TMP_Text textbox in textboxes) * { * if (textbox.name == "Countdown") * { * textbox.text = "" + Countdown; * } * } */ // update countdown label CountdownTextboxes[SelectedLevel - 1].text = "" + Countdown; CountdownTimer.Start(); Countdown1.PlayOneShot(Countdown1Sound); } // check if a second has passed. // if yes, countdown and update the right textbox to show // also, play a sound based on how much time is left if (CountdownTimer.TimerFinished()) { Countdown--; /* * // old method of updating labels, was excessive * TMP_Text[] textboxes = Planets[i].GetComponentsInChildren<TMP_Text>(); * foreach (TMP_Text textbox in textboxes) * { * if (textbox.name == "Countdown") * { * textbox.text = "" + Countdown; * } * } */ // update countdown label CountdownTextboxes[SelectedLevel - 1].text = "" + Countdown; if (Countdown == 0) { Countdown2.PlayOneShot(Countdown2Sound); SceneManager.LoadScene(SelectedLevel); } else { Countdown1.PlayOneShot(Countdown1Sound); } } } else { // reset countdown Countdown = 3; CountdownTimer.Stop(); } }
private void Update() { // if the ship is alive, check if the game has not started // if not started, wait until the player hovers over the ship for 3 seconds // otherwise, have the ship follow the cursor and fire lasers occasionally if (IsAlive) { if (!HasStarted) { // game has not begun, make sure that the mouse is close to ship, then countdown Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 distanceToMouse = transform.position - mousePosition; if (distanceToMouse.magnitude <= 1) { if (StartTimer.IsRunning()) { StartTimer.IncrementTime(Time.deltaTime); } else { StartTimer.Start(); CountdownLabel.text = "" + StartCountdown; Countdown1.Play(); } if (StartTimer.TimerFinished()) { StartCountdown--; CountdownLabel.text = "" + StartCountdown; if (StartCountdown == 0) { HasStarted = true; CountdownLabel.text = ""; Countdown2.Play(); } else { Countdown1.Play(); } } } else { StartTimer.Stop(); StartCountdown = 3; CountdownLabel.text = ""; } } else { // game has started // ship movement and laser firing ShotTimer.IncrementTime(Time.deltaTime); if (ShotTimer.TimerFinished()) { FireShot(); } ChaseMouse(); } // check for out of bounds if ((transform.position - Vector3.zero).magnitude > 10f) { Debug.Log("You have gone too far."); DestroyShip(); // to loss screen } // check to see if all enemies have been defeated if (FindObjectsOfType <EnemyBehaviors>().Length == 0) { Debug.Log("You won"); SceneManager.LoadScene(5); // to win screen } } else { // game over, shouldn't reach this } }