Coin.cs
Assets > Scripts > Coin.cs
Assets > Scripts > Coin.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
public float spinSpeed = 100f;
public int xpValue = 1;
[SerializeField] private AudioSource WorldSFXSource;
[SerializeField] private AudioClip CoinCollect;
private void Start()
{
WorldSFXSource = WorldSFXSource.GetComponent<AudioSource>();
}
void Update()
{
this.transform.Rotate(0f, 0f, Time.deltaTime * this.spinSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
other.gameObject.GetComponent<PlayerControllerThatLevelsUp>().GainXP(xpValue);
Destroy(this.gameObject);
WorldSFXSource.PlayOneShot(CoinCollect);
}
}
}
This code will be explained in sections, which each have a different colour to separate them from each other. Comments in the script have been removed from this excerpt, but will be included in the Google Doc containing the script, linked at the bottom of the page.
Note that in this script and in subsequent scripts, I'll be explaining what the functions are doing in the script rather than explaining what they are in general (except for functions that I haven't already explained before), which is what I did with the PlayerController.cs page. If you want to see my explanation for the functions in general, you can access that page with the button below.
SECTION 1: Setting up the variables
This section is dedicated to setting up the script-wide variables that will be used in the script.
public class Coin : MonoBehaviour
{
public float spinSpeed = 100f
public int xpValue = 1
[SerializeField] private AudioSource WorldSFXSource
[SerializeField] private AudioClip CoinCollect
Just before the script is about to start, I've set up four variable for three different functions in the script. First is the float spinSpeed, which is used in its relevant function to determine how fast the coin should rotate. Second is the int xpValue, which is used in its relevant function to determine how much each coin is worth in terms of XP points. The third and fourth are the Audio Source and Audio Clip that the script will use to play a coin sound effect when the player picks up the coin.
[SerializeField]
The SerializeField attribute serializes the script's components, allowing you to tweak them in the inspector, which is an ability that is removed when you set a variable as private. This is needed for the third and fourth variable as I need to attach the Audio Source and Audio Clip to this script so it knows what to use.
SECTION 2: Defining what calling the variable alone does
This section is dedicated to defining what calling the variable alone does so that it doesn't have to be done every time the variable is called in the script.
private void Start()
{
WorldSFXSource = WorldSFXSource.GetComponent<AudioSource>()
}
This function is basically telling the script what to do when the variable WorldSFXSource is called on its own (for example, it allows me to do WorldSFXSource.Play() instead of WorldSFXSource.GetComponent<AudioSource>().Play(), which can really become a handful when you're using this single variable multiple times. I chose to use it in this script because of personal choice because I like organizing my script a bit, even if the variable is only used once, but it's not necessary for the script to work in this case.
A flow chart that shows the order of operations for each function in this section.
SECTION 3: Making the coin spin
This part in the script is what makes the coin spin in the game.
void Update()
{
this.transform.Rotate(0f, 0f, Time.deltaTime * this.spinSpeed)
}
This is where the float created before, spinSpeed, comes into play. This function gets the transform of the GameObject that this script is attached to (in this case, the coin), and rotates it on the Z axis with an equation of Time.deltaTime * this.spinSpeed. To turn the equation into numbers, we find that Time.deltaTime uses frame timing figures (for example, the game running at 60 FPS would have an average of 17 ms frame timing, you can find this by printing Time.deltaTime to the debug console in Update), which means that the equation in this case would be 0.017 * 100, which equals 1.7. Putting that into an equation for the rotation per seconds (100 * 1.7) means that the coin rotates around 170° on the Z axis every second.
See now, my math skills ain't too bad. It's as simple as 2 + 2, which equals 5.
A flow chart that shows the order of operations for each function in this section.
SECTION 4: Allowing the player to pick up the coin
This section of the script allows the player to pick up the coin and communicates with the PlayerControllerThatLevelsUp.cs script to give the player XP.
private void OnTriggerEnter(Collider other)
This method in the script will activate once the collider of another object enters the trigger collider of the coin (only if the collider on the coin is actually set as a trigger with "IsTrigger"). The collider variable is set to "other", which means it's not set to any specific variable that we've defined in Section 1.
if (other.gameObject.tag == "Player")
This if statement checks to see if the object it has just collided with has the tag "Player". If it finds that it has collided with this tagged object, it'll do the functions listed below it, and if not, it'll do nothing. The only object in this context that is tagged as "Player" is our Player Controller.
other.gameObject.GetComponent<PlayerControllerThatLevelsUp>().GainXP(xpValue)
Destroy(this.gameObject)
WorldSFXSource.PlayOneShot(CoinCollect)
This is where the xpValue int set in Section 1 comes into play.
If the criterion for the if statement above is met, then it will call these functions. The first thing that happens is it will access the PlayerControllerThatLevelsUp script that the Player Controller has and will add the amount defined in xpValue (by default, 1). It will then destroy the GameObject that the script is attached to, the coin, and then finish off the script by playing the Audio Clip that is attached to the CoinCollect variable defined above using WorldSFXSource.
The full script
This the full Coin.cs script, straight from the project. This version of the script includes the original colouring and also has the original comments that were removed from the code above.