Unity C# Quest Scripts

 Introduction

The C# Unity scripts I wrote for the Uncle Sam Plantation project create a basic quest structure that guides players through a 3D game or simulation. My research into 3D digital game-based language learning environments show that goal-directed player activity in these environments provide learners of a second language and culture with a more nuanced view of the activity systems that constitute a target culture, and also apparently influences how learners invoke and structure language in order to describe these systems. I am curious to find out if this type of learning can be applied to other academic disciplines such as American History and, if yes, how this learning can be implemented in a classroom setting. The Uncle Sam Plantation project is, I hope, a step in this direction.

The scripts are cobbled together from numerous books and online courses that I read and took: Advanced Unity 3D Game Programming with Michael House, Scripting Unity with C# with Kelley Hecker, Learning C# Programming with Unity 3D by Alex Okita, Unity 5.x Cookbook by Matt Smith and Chico Queiroz, and Unity in Action: Multiplatform Game Development in C# with Unity 5 by Joseph Hocking. All project assets, including C# scripts and 3D models, can be downloaded from my GitHub project repository under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

Script Functionality

All scripts related to game quest functionality can be found in the Quest Manager folder (Assets → Scripts → Quest Manager). When the game is launched, the QuestLoader.cs script reads in quest data from flat database text files (Assets → Quests), parses the data according to line headers found in the text files, and adds data to a struct (Quest.cs) organized in a dictionary. Once the files have been read in, the QuestLoader.cs script will push a dictionary containing quest information to GameManager.cs and, if no active quest has been set, will set the active quest to the information contained in the first data file. If the read and push is done for the first time, this will result in a lazy initialization of a GameManager.cs object. The GameManager.cs script inherits from the Singleton.cs script, which ensures that only one game manager object is created for monitoring game state. The game manager object persists through scene destruction and loading.

Before the PlayerInterface.cs script can be used, the FPSController prefab needs to be loaded into the project. Once that has been done, the PlayerInterface.cs script needs to be attached to the FirstPersonCharacter component of this prefab. This script gets the camera attached to this component, reads in the active quest from the game manager object, and creates a ray that is projected from the camera into the scene. If the ray intersects an object, the PlayerInterface.cs script checks to see if this object is a current quest object and, if yes, gets quest information from the object and sends it to the game manager object. Different information is sent based on whether the ray is entering or leaving the object, and whether left mouse button has been activated. The PlayerInterface.cs script also has a function to display the GUI dot that visually corresponds with the ray that is cast into the scene and a coroutine for pausing the text that is displayed on the screen.

3D models that have been imported into the project can be made into quest objects by attaching the InteractiveObject.cs script to them, creating a flat database quest file for them, and assigning the correct quest name to them in the Inspector interface in Unity (must correspond with the “questObject” line header in the the quest file). In this manner a chain of interactive objects can be created that forms a basic linear quest. The script also contains functions for reading in quest data from the game manager object and object shaders. If an current quest object is moused over, for example, a glow shader will be applied to the object, indicating that the player can interact with it.

Before the UpdateUI.cs script can be used, a new Canvas object needs to be created in the project. Once this has been done the UpdateUI.cs script can be attached to it, ensuring that all text generated by player interaction with objects can be displayed on the screen.

Research and Teaching Directions

I am interested in growing the project and welcome anybody who would like to get involved. I could certainly use help from subject matter experts in the fields of American History, American Studies, or African-American Studies. People interested in contributing to the C# Unity code are also welcome. I would be interested in expanding the codebase to include some multiplayer functionality, branching dialogue trees, or more complex quest structures. Or, if there is something that you see needs doing, please feel free to contribute that, too. There are probably some Unity extensions that can be purchased for this, but I would like to develop as much as possible in-house.

If you don’t want to contribute to the project but would like to use the codebase for your own project, that’s also okay. Please download the code or fork it from my GitHub repository and dig in. I also think that the code could easily be used in an instructional setting such as a semester capstone experience or group project. For example: Students could create their own models in SketchUp, import these into Unity, apply the code, and then create a quest that would lead players through the environment.

One thought on “Unity C# Quest Scripts

Leave a comment