Time for action – creating a good and bad prefab

We need to create two prefabs. The names aren't too important because we'll be using tags in our code. The tag names are important, which are GoodOrb and BadOrb.

  1. Add two GameObjects of your choice.
  2. I suggest naming them GoodOrb and BadOrb.
  3. To GoodOrb, add a tag name of GoodOrb.
  4. To BadOrb, add a tag name of BadOrb.
  5. Create a prefab of each.
  6. Randomly add about 5 of each prefab to Scene1 and Scene2.
  7. As an example, see the screenshot at the beginning of this chapter.

What just happened?

We now have the ability to increase our Score or lose Lives

Scoring for the win

All it takes to win is for the value in the variable score to be 2.

To increase score, Player will collide with a GoodOrb which increases score by 1, and GoodOrb will disappear from the Scene.

Scoring for the win

An analysis of the code we saw in the preceding code screenshot is as follows:

Line 38: void OnTriggerEnter(Collider other)

  • This is a copy and paste from the example code in the Scripting Reference
  • The Player Box Collider is check as being Is Trigger
  • When the Player Box Collider runs into the another GameObject with a Collider, Unity calls the OnTriggerEnter() method
  • The variable other now stores a reference to the Collider that Player ran into

Line 40: if(other.gameObject.tag == "GoodOr b")

  • We are checking to see if the GameObject that the other Collider is attached to has a tag named GoodOrb

Line 42: gameDataRef.score += 1;C

  • score is increased by 1

Line 43: Destroy(other.gameObjec t);

  • The Destroy() method is removing GoodOrb from the Scene

Losing when Player crashes

All it takes to lose is for the value in the variable playerLives to be 0.

To decrease playerLives, Player will collide with a BadOrb. The playerLives variable will decrease by 1, and the BadOrb will disappear from the Scene.

Losing when Player crashes

An analysis of the code we saw in the preceding code screenshot is as follows:

This code is similar to OnTriggerEnter(). However, since we are using OnCollisionEnter(), we have to use a Collider that does not have Is Trigger checked. The child object of Player is the Sphere which has a Box Collider. When this Collider crashes into a BadOrb, Player will lose a life.

Line 47: void OnCollisionEnter(Collision collidedWith)

  • When Player runs into another GameObject with a Collider that is not a trigger, Unity automatically calls OnCollisionEnter()
  • The collidedWith variable now stores a reference to the other Collider that Player ran into
  • Notice that collidedWith is a Collision type, not a Collider
  • Collisions, as opposed to triggers, require using the Collision class

Line 49: if(collidedWith.gameObject.tag == "BadOrb")

  • This checks if the GameObject Player collided with has a tag named BadOrb

Line 51: gameDataRef.playerLives -= 1;

  • playerLives is decreased by 1

Line 52: Destroy(collidedWith.gameObject);

  • The Destroy() method removes BadOrb from the Scene

There is a way to increase Player Lives. By shooting a BadOrb, the variable playerLives will be increased by 1. It also turns a BadOrb into a GoodOrb.

..................Content has been hidden....................

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