Displaying inventory texts for single object pickups

Many games involve the player picking up items. Perhaps the most common is the need to pick up a key to be able to pass through a doorway, as shown in the following screenshot:

Displaying inventory texts for single object pickups

Getting ready

In the 0423_04_11 folder you'll find some key images, and a crisscross image to apply to the terrain.

How to do it...

To display a text description of inventory objects, please follow these steps:

  1. Create a new scene, and add a directional light.
  2. Create a new terrain with Size set to 2000 x 2000 and Position (-1000, 0, -1000). Now, apply a texture to this terrain.
  3. Create a cube named Cube – key at position (0, 1, 5) with scale (2, 2, 2).
  4. Tag Cube–key with the string key, and tick its Is Trigger checkbox, as shown in the following screenshot:
    How to do it...
  5. Add a key image to Cube–key.
  6. Import the built-in Character Controller Unity package and add a 3rd Person Controller to your scene at position (0, 1, 0).
  7. Attach the following script class to your character controller:
    // file: PlayerInventory
    using UnityEngine;
    using System.Collections;
    
    public class PlayerInventory : MonoBehaviour 
    {
      private bool isCarryingKey = false;
      
      private void OnGUI()
      {
        string keyMessage = "(bag is empty)";
        if( isCarryingKey )
        {
          keyMessage = "carrying: [ key ]";
        }
        
        GUILayout.Label ( keyMessage );
      }
      
      private void OnTriggerEnter(Collider hitCollider)
      {
        if( "key" == hitCollider.tag )
       {
          isCarryingKey = true;
          Destroy ( hitCollider.gameObject );
        }
      }
    }

How it works...

The isCarryingKey Boolean variable represents whether or not the player is carrying the key at any point in time.

In the OnGUI() method the contents of the keyMessage string is displayed via the GUILayout.Label() method. The default value of this string tells the user that the players bag is empty, but an if statement tests the value of isCarryingKey. If that is true, then the message is changed to inform the user that the player is carrying a key.

The OnTriggerEnter() method tests the tag string of any object the player's character controller collides with that has its Is Trigger checkbox set to true.

Each time the player's character controller collides with any object that has its Is Trigger value set to true, an OnTriggerEnter() event message is sent to both of the objects involved in the collision.

The OnTriggerEnter() message is passed a parameter, which is the Collider component inside the object it just collided with. Our character controller's OnTriggerEnter() method tests the tag string of the object it collided with to see if has the key value.

Since the Cube–key cube that we created has its trigger set, and has the key tag, then the if statement inside this method will detect a collision with Cube–key and will perform the following two actions:

  • This method sets the isCarryingKey Boolean variable to true
  • It also destroys the game object it has just collided with (in this case, Cube–key).

Note

Boolean variables are often referred to as flags

The use of a bool (true/false) variable, to represent whether a particular feature of the game state is true or false, is very common. Programmers often refer to these variables as "flags". So programmers might refer to the isCarryingKey variable as the key carrying flag.

There's more...

Here are some details you don't want to miss:

Comparing the gameObject tag with a string special method

Comparing the tag string of a game object against a string is such a common task that Unity has provided a special method for just this purpose. This method is named CompareTag(), and takes a string as its single argument. We can therefore rewrite the if statement in the OnTriggerEnter method a little more elegantly, as follows:

if( hitCollider.CompareTag("key") )

See also...

  • The Displaying inventory icons for single object pickups recipe.
  • The Managing inventories with a general purpose PickUp class recipe.
..................Content has been hidden....................

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