Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using UnityEngine.SceneManagement;
- public class FleetScript : MonoBehaviour
- {
- //how often does the fleet move
- public float moveTime = 2;
- //speed
- public float moveSpeed;
- //move counter
- int moveCounter = 0;
- //movement direction
- int moveDir;
- public bool gameRun;
- public TMP_Text endText;
- void Start()
- {
- moveDir = 1;
- StartGame();
- }
- void Update()
- {
- if(gameRun && transform.childCount == 0)
- {
- //If the game is still on and the fleet doesn't have aliens anymore we stop the game
- StopGame(true);
- }
- //resetting the game when the player presses R
- if (Input.GetKeyDown(KeyCode.R)) SceneManager.LoadScene(0);
- }
- public void StopGame(bool win)
- {
- //Stop the game and movement
- CancelInvoke("Move");
- gameRun = false;
- //Switch on the text and fill it appropriately
- endText.gameObject.SetActive(true);
- endText.text = win ? "You Win!" : "You Lose!";
- }
- public void StartGame()
- {
- //sterting calling a method with specified interval
- InvokeRepeating("Move", moveTime, moveTime);
- //starting the game and disabling the text
- gameRun = true;
- endText.gameObject.SetActive(false);
- }
- void Move()
- {
- //depending on the direction change the position
- transform.position += new Vector3(moveSpeed * moveDir, 0, 0);
- moveCounter += moveDir;
- //Move down if the counter goes above 4 or below -4
- if (moveCounter <= -4 || moveCounter >= 4)
- {
- moveDir *= -1;
- transform.position -= new Vector3(0, moveSpeed, 0);
- }
- }
- }
Add Comment
Please, Sign In to add comment