Progressive coin collecting sounds, Unity 3D

by on under Code snippets
1 minute read

I recently bought and played a game named Godus. I’ve seen an interesting sound effect and I will try to replicate it into unity. (example)

Here is the GameManager.cs:

public static int coinSoundOrder = 0;
public static float t=0;
public static AudioClip[] sound;
public AudioClip[] pSound;
public float expireTime = 2f;
void Start(){
	sound = pSound;
}
public static void PlayCoinSound(){
	AudioSource.PlayClipAtPoint(sound[coinSoundOrder], new Vector3(0,0,0));
	if(coinSoundOrder < sound.Length - 1)
		coinSoundOrder++;
	else
		coinSoundOrder = 0;
	t=0f;
}
void Update(){
	t+=Time.deltaTime;
	if(coinSoundOrder > 0 && t>=expireTime)
		coinSoundOrder=0;
}

* I’ve added a new variable, pSound, because I can’t find a way to declare a static variable that can be edited in the inspector.
And now you can call that function each time the player hits a coin, for example:

void OnTriggerEnter2D(Collider2D obiect){
	if(obiect.gameObject.tag == "Coins"){
		GameManager.PlayCoinSound ();
		Destroy(obiect.gameObject);
	}
}
unity 3D, code snippet, progressive, coin collecting, sound
comments powered by Disqus