My journey becoming a Unity game developer: 2.5D Infinite Runner-Updating the Coin System
Objective: Updating the Coin prefab prototype to have a coin object rendered in place of the sphere used. Also, make the coins rotate around until the Player collects them and make a Parent to the Coin prefab which will allow the coins to work properly in the game.
We’re going to add a way for the player to receive points as they move through the game by grabbing coins. We’re going to use the Coin prefab created during the prototyping of this level.
Drag the Coin prefab into the Scene view and place it in front of the Player character. The Coin prefab is a Sphere which was used during prototyping. Go to the Sphere (Mesh Filter) and select the Circle_006 which will now be referenced for the Mesh Renderer to render this mesh onscreen. If needed, either create a new Material or use a Material already created under Mesh Renderer in Materials to represent the color of the Coin. Also, rotate the Coin 90 degrees along the Y-axis to face the Main Camera. We should have a Rigidbody with Use Gravity is turned off, and the Coin script attached to this Coin prefab from the prototype.
Now we’re going to make the Coin rotate indefinitely. However, we have a problem with this coin in that the direction of the Coin is not aligned with the game. If we look closer, the Coin Z-axis is facing the Camera and the X-axis is in the forward direction. This will cause our Coin to move in a different way than we want, therefore we’re going to create a Parent Game Object name Coins.
Copy the Coin Transform component and Paste the Component values onto the Coins Transform component. Then drag the Coin prefab into the Coins game object and Reset the Coin prefab to zero along the Transform Positions. We need to add the Sphere Collider, Rigidbody with Use Gravity turned off, and Coin script to the Coins game object. However, with the Coin game object we need to Remove Component of the Sphere Collider, Rigidbody, and Coin script since we don’t need it here anymore.
Final thing we need to do with the Coins game object is to drag it into the Prefabs folder to make it officially a prefab.
As we play the game, we can see the Player is able to collect the coins with the Coins game object displayed with a new coin shaped Mesh Filter attached to the child Coin prefab game object.
Let’s make the coins rotate by going in the Coin script. Create a new variable that is Public in the Inspector Type Float name rotateSpeed. Using the Fixed Update() function, we’re going to get the transform of the coin and use the Rotate() method. Inside the Rotate() method, we will rotate the coin around the Y-axis using Vector3.up and use the rotateSpeed variable for the speed at which the coin will rotate around.
Play the game and watch the coins rotating in the game until the Player grabs them.