官术网_书友最值得收藏!

Putting it all together

Now that we have developed the classes necessary for the InventoryMgr, MissionMgr, and InteractiveObj systems, let's build a scene that illustrates their functionality.

  1. To get started, let's continue from Chapter 1, Introduction to E-Learning and the Three Cs of 3D Games, where we left off. Load the scene and save a copy named chapter2.
  2. Add an instance of InventoryMgr to the Player GameObject by dragging-and-dropping it from the Project tab to the player object. Alternatively, click on Player and select Add component from the Inspector pane. Type in the name InventoryMgr and then click on it to add an instance.
    Putting it all together
  3. Click on Player and ensure that the Tag is set to Player. The InteractiveObj class will use this to make sure they can only be picked up by the player.
  4. Set Height and Width to 40 pixels to make the individual cells square shaped. Set the Yposition to 0.85 to indicate a y coordinate of 85 percent from the top of the screen.
  5. Create a new empty GameObject and name it MissionMgr. Attach the MissionMgr script to this object. We purposely detach this script from the player because the lifespan of MissionMgr may differ from the player object.

    The results of all the discussed settings are shown in the following screenshot:

    Putting it all together

Now that we have added our new tracking systems to the game, let's create some objects to interact with:

  1. Create a sphere and place it on the ground plane. Name it A_Coin and disable the mesh renderer component. On the sphere collider, click the IsTrigger checkbox so that we can detect when the player enters the object.
  2. Add a Rigidbody component to the object. This is necessary for the OnTriggerEnter callback to be dispatched by the engine. Remember, the way Unity detects when the player (or any other object) enters a trigger is by checking its Rigidbody component; so, if there is no such component attached, the the callback will not be dispatched.
  3. Add a CustomGameObject script to this object. Set the display name to money and the object type to coin.
  4. Add a MissionToken script to this object. Set id to 1, Title to token, and the Description to mission 1 token.
  5. Add an InteractiveObj script to this object. Set Rot Axis to (0, 1, 0) to make the object rotate horizontally about the y axis. Set Rot Speed to 40 for a gentle rotation.
  6. Add an ObjectInteraction script to this object. Set the interaction to putInInventory. Set the interaction type to accumulate. Point Tex to the Coin-icon coin texture.
  7. Drag-and-drop this gameObject into the OnCloseEnough variable of the InteractiveObject component. This will connect this reference to the ObjectInteraction component on gameObject itself.
  8. Now that the script is wired up, let's add a yellow coin model to the coin base. Create a cylinder gameObject, parent it to the A_Coin base, and set its local position to (0, 0, 0). Create a material, colored yellow, and attach it to the mesh renderer of the cylinder.
  9. Set its rotation to (90, 0, 0) in the inspector. Set its scale to (0, 0.16, 0) in the inspector as well.

    The result of all these steps can be seen in the following screenshot:

    Putting it all together

Congratulations! We now have one interactive object in the world. To test this, let's click on play and observe a slowly rotating coin. If we then walk the player up to it so that he or she can collect it, we will then see the inventory displaying the item.

Testing the mission system

Let's add another type of interactive object and then build a couple of missions to finish validating our work.

  1. Drag-and-drop this A_Coin object from the Hierarchy tab into the Project tab, thereby creating a Prefab. Name this Prefab coin, as shown in the following screenshot:
    Testing the mission system
  2. Create a second interactive object, following the steps 1 to 9 from the Building an interactive object section. Instead of making a yellow coin, create a red ruby and point the inventory texture to the ruby icon texture provided.
  3. Name this object A_Ruby and set the mission token ID to 4.
  4. Drag-and-drop A_Ruby to the Project tab, forming a Prefab named Ruby.
  5. Drag-and-drop the coin and ruby Prefabs back into the Hierarchy tab. At this point, there should be two coins and two rubies. Set the mission token ID of the second coin to 2 and the second ruby to 4.

Now that we have two coins and two rubies, let's create two missions!

  1. Click on MissionMgr and increase the size of the missions' array to 2. Note that the array populates with an empty version of the Mission class. Set the first mission to activated and visible.
  2. Set the first mission state to MS_ACQUIRED and name it Coin Craze.
  3. Set the description to collect all of the coins.
  4. Set the points to 500.
  5. Under tokens, drag-and-drop the two coin game objects. This will set the references to MissionTokens attached to the coin Prefabs.
  6. Create a reward Prefab out of GuiText with SimpleLifeSpanScript attached. Set the string to You have completed the coin challenge with a lifespan of 2 seconds. Drag-and-drop this Prefab into the reward field of mission 1 in the mission manager. Make sure to delete any instances of this reward Prefab from the hierarchy; we want MissionMgr to spawn this!
    Testing the mission system
  7. Follow steps 7 to 12 from the Building an interactive object section again, only this time name the mission Ruby Run and point the mission tokens to the two ruby instances in the world (with unique mission token IDs). Create a new reward for this mission and drag-and-drop its Prefab onto the reward for mission 2.
  8. Position the coins and rubies in the world at random locations in the game world. Play the level to observe MissionMgr in action, as shown in the following screenshot:
    Testing the mission system
主站蜘蛛池模板: 武强县| 上饶市| 延庆县| 宁安市| 车险| 华宁县| 璧山县| 牙克石市| 曲沃县| 团风县| 三河市| 安阳县| 三河市| 闽侯县| 乡城县| 宜兴市| 共和县| 南雄市| 泊头市| 台前县| 通化县| 田东县| 平罗县| 大化| 江源县| 衡阳市| 东方市| 甘肃省| 涿鹿县| 洪洞县| 昆山市| 靖江市| 曲阳县| 扎鲁特旗| 凉城县| 日照市| 根河市| 景谷| 肇州县| 逊克县| 文水县|