- Creating ELearning Games with Unity
- David Horachek
- 971字
- 2021-07-16 12:22:51
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.
- 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
. - Add an instance of
InventoryMgr
to thePlayer
GameObject by dragging-and-dropping it from the Project tab to theplayer
object. Alternatively, click on Player and select Add component from the Inspector pane. Type in the nameInventoryMgr
and then click on it to add an instance. - 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. - 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.
- Create a new empty GameObject and name it
MissionMgr
. Attach theMissionMgr
script to this object. We purposely detach this script from the player because the lifespan ofMissionMgr
may differ from the player object.The results of all the discussed settings are shown in the following screenshot:
Now that we have added our new tracking systems to the game, let's create some objects to interact with:
- 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. - 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. - Add a
CustomGameObject
script to this object. Set the display name tomoney
and the object type tocoin
. - Add a
MissionToken
script to this object. Set id to1
, Title totoken
, and the Description tomission 1 token
. - 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 to40
for a gentle rotation. - Add an
ObjectInteraction
script to this object. Set the interaction toputInInventory
. Set the interaction type toaccumulate
. PointTex
to theCoin-icon
coin texture. - Drag-and-drop this
gameObject
into theOnCloseEnough
variable of theInteractiveObject
component. This will connect this reference to theObjectInteraction
component ongameObject
itself. - 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 theA_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. - 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:
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.
- Drag-and-drop this
A_Coin
object from the Hierarchy tab into the Project tab, thereby creating a Prefab. Name this Prefabcoin
, as shown in the following screenshot: - 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.
- Name this object
A_Ruby
and set the mission token ID to4
. - Drag-and-drop
A_Ruby
to the Project tab, forming a Prefab namedRuby
. - 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 to4
.
Now that we have two coins and two rubies, let's create two missions!
- Click on
MissionMgr
and increase the size of the missions' array to2
. Note that the array populates with an empty version of theMission
class. Set the first mission toactivated
andvisible
. - Set the first mission state to MS_ACQUIRED and name it Coin Craze.
- Set the description to collect all of the coins.
- Set the points to 500.
- Under tokens, drag-and-drop the two coin game objects. This will set the references to
MissionTokens
attached to the coin Prefabs. - Create a reward Prefab out of
GuiText
withSimpleLifeSpanScript
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 ofmission 1
in the mission manager. Make sure to delete any instances of this reward Prefab from the hierarchy; we wantMissionMgr
to spawn this! - 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.
- 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:
- 我的J2EE成功之路
- Practical Ansible 2
- Practical Data Wrangling
- Natural Language Processing Fundamentals
- Getting Started with MariaDB
- CorelDRAW X4中文版平面設(shè)計(jì)50例
- 機(jī)器學(xué)習(xí)流水線實(shí)戰(zhàn)
- DevOps:Continuous Delivery,Integration,and Deployment with DevOps
- CompTIA Network+ Certification Guide
- 人工智能趣味入門:光環(huán)板程序設(shè)計(jì)
- Machine Learning with the Elastic Stack
- Chef:Powerful Infrastructure Automation
- Unity Multiplayer Games
- 智能制造系統(tǒng)及關(guān)鍵使能技術(shù)
- JRuby語(yǔ)言實(shí)戰(zhàn)技術(shù)