Simulating the collection of cherries

In this section, we will continue working on the CucumberManManager script to simulate the collection of cherries.

If you do not already have the script open, open it now in an editor. We will review the updated code in five sequential sections.

Our first section, shown here, imports the three necessary namespaces.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CucumberManManager : MonoBehaviour {

public static int currentCherryCount;
public int tempCurrentCherryCount;
public bool collectingCherries;

The fourth line of code is the class declaration statement. The last three lines of code in this section are the class variables. Here is a brief description of each:

  • currentCherryCount: Keep the current number of cherries in the Cucumber Man's inventory
  • tempCurrentCherryCount: Used to limit cherry collection to one per second
  • collectingCherries: Used to determine whether the inventory counter should be active

Our second section, shown as follows, features the Awake() method. This method is used to initialize our variables at the start of the game:

     void Awake () {

currentCherryCount = 0;
tempCurrentCherryCount = 0;
collectingCherries = false;

}

Our third section, shown here, features the Update() method. This method is executed once per frame. We have an if/else statement nested in an outer if statement. This outer statement checks to see whether the collectingCherries Boolean value is true. If it is, the inner if/else statement block is evaluated.

The inner if/else block checks to see whether the tempCurrentCherryCount variable's value is greater than or equal to 60. If it is, then the currentCherryCount value is incremented by one; otherwise, the tempCurrentCherryCount value is incremented by one. The Update() method is called once per frame and frame rates can vary. So, we are essentially adding one cherry to the Cucumber Man's inventory every 60 frames:

     void Update () {

if (collectingCherries) {
if (tempCurrentCherryCount >= 60) {
currentCherryCount = currentCherryCount + 1;
tempCurrentCherryCount = 0;

} else {
tempCurrentCherryCount = tempCurrentCherryCount + 1;
}
}

}

Our fourth section, shown in the following code block, contains the OnTriggerEnter() method we started in the last section. We edited this method to include an if statement that checks to see whether the Cucumber Man has entered a collision with a cherry tree. If so, the collectingCherries Boolean variable is set to true and one cherry is added to the inventory:

     void OnTriggerEnter(Collider theObject) {
if (theObject.gameObject.CompareTag ("CherryTree")) {

collectingCherries = true;
currentCherryCount = currentCherryCount + 1;
}
}

Our fifth section, shown as follows, has the OnTriggerExit() method. This event is fired when the Cucumber Man stops colliding with the cherry trees. When this occurs, we set the collectingCherries Boolean value to false:

 
void OnTriggerExit(Collider theObject) {
if (theObject.gameObject.CompareTag ("CherryTree")) {
collectingCherries = false;
}
}

}
// end of CucumberManManager.cs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset