Part 5: Arrow System

Christiane Snyder

Using the coin prefab and associated concepts from Part 4, we’ll create the aforementioned system that keeps track of all the coins in the game.

Create an empty game object named “GameSystem” in the scene.

Then create a C# script called “ArrowSystem” and attach it to your “GameSystem” object.

As was mentioned before, we don’t want the user to be present in the experience for extended periods of time. To ensure playing time is relatively short, we’ll spawn a small number of coins to be collected. The example used here is five.

Instead of setting the collected coin to inactive in the “MainFish” script, change it’s “OnCollisionEnter” function to the following, so that control of the coins and arrows is kept inside of the “ArrowSystem” script.

Create two public GameObject variables named Arrow and Coin. Then drag the prefabs in the project by the same names to their matching fields in the “ArrowSystem” options.

Once you have “Arrow” and “Coin” variables set to valid objects, instantiate the game objects for both arrays and set entries 1-4 to inactive:

NOTE: line 20 should read:

Coins[2] = (GameObject)Instantiate (Coin, new Vector3(-20.0f,2.0f,2.0f), Quaternion.Euler(90, 0, 0));

You can now revise “UpdateArrows” so that it activates and deactivates entries in the two game object arrays based on how many coins have been gathered:

At this point, one arrow and coin will be active in the hierarchy at any given time, but the active arrow does not currently point to the active coin. Rotate the arrows so they always aim down the vector connecting them to their corresponding coins.

The following changes to “ArrowSystem”s Start() function will correct the arrows rotations:

NOTE: on the arrow models, their right vector is what would be considered forward for use in this tutorial, so we rotate the Arrow by Vector3(0f,-90f,0f).

The arrows now act as waypoints guiding players to their next destination.

Up until now, the arrows’ locations are just set to the last active coins’ position. Since the activation of a new arrow-coin pair is dependant on the fish colliding with a coin and the fish/ camera are continuously moving forward, the arrow is reactivated very close to the camera and is difficult to see.

This can be fixed by calculating an arrow’s location relative to the “Main Camera” directly after collision with a coin. With this modification, the new arrow will be easy to view once activated because it is offset a suitable distance along the camera’s forward vector.

Change the “UpdateArrows” function in the “ArrowSystem” script so that it takes in a Vector3 (this value will represent the next arrow’s location):

We’ll edit this function so that the new arrow’s position is set to the passed in variable and its rotation is updated accordingly by calculating its LookRotation using this value.

Our coin prefab’s up vector is perpendicular to the flat circular side of the cylinder. So we want to set it’s up vector(instead of forward) to the direction vector from the coin’s location to its affiliated arrow’s location.

Upon collision with a coin in “MainFish”, calculate the coordinate point directly in front of the Main Camera that is five units away. Then pass this position into “UpdateArrows”:

At this point, you should have a fully functioning arrow system that activates and deactivates one coin and arrow at a time.