Creating Gameplay Controller

Submitted by: 
Operating System: 
Visitors have accessed this post 553 times.

Now we will create the Gameplay Controller. This will be the game mechanics of the game. It will handle the flow of the game to make it playable. Create a GameObject then name it as Gameplay Controller. Then create a script and save it to Game Controllers folder as GameplayController.

Write these important variables to make the game work properly:

  1. public static GameplayController instance;
  2.  
  3. public int score;
  4. public Text scoreText;
  5. public GameObject notification;
  6.  
  7. private bool doubleBack;

Then write the rest of the codes

  1. void Awake(){
  2. CreateInstance ();
  3. }
  4.  
  5. // Use this for initialization
  6. void Start () {
  7. if(GameController.instance != null && MusicController.instance != null){
  8. if (GameController.instance.isMusicOn) {
  9. MusicController.instance.PlayGameplaySound ();
  10. } else {
  11. MusicController.instance.StopAllSound ();
  12. }
  13. }
  14. InitialGameplayVariables ();
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19. UpdateGameplayController ();
  20.  
  21. if(doubleBack == true){
  22. if(Input.GetKeyDown(KeyCode.Escape)){
  23. SceneManager.LoadScene ("Main Menu");
  24. }
  25. }
  26.  
  27. if(Input.GetKeyDown(KeyCode.Escape)){
  28. notification.SetActive (true);
  29. doubleBack = true;
  30. StartCoroutine (ShowTimer ());
  31. }
  32. }
  33.  
  34. void CreateInstance(){
  35. if(instance == null){
  36. instance = this;
  37. }
  38. }
  39.  
  40. void UpdateGameplayController(){
  41. GameController.instance.currentScore = score;
  42. scoreText.text = score.ToString ();
  43. }
  44.  
  45. void InitialGameplayVariables(){
  46. GameController.instance.currentScore = 0;
  47. score = GameController.instance.currentScore;
  48. scoreText.text = score.ToString ();
  49. }
  50.  
  51. IEnumerator ShowTimer(){
  52. yield return new WaitForSeconds (2f);
  53. doubleBack = false;
  54. notification.SetActive (false);
  55. }

After creating the script attach it to the Gameplay Controller component. And then attach all the needed components in the Inspector of GameplayController script.
tut37


Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.