This post contains all the code that’s been written in this YouTube video.
PuckScript.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PuckScript : MonoBehaviour { public ScoreScript ScoreScriptInstance; public static bool WasGoal { get; private set; } public float MaxSpeed; public AudioManager audioManager; private Rigidbody2D rb; // Use this for initialization void Start () { rb = GetComponent<Rigidbody2D>(); WasGoal = false; } private void OnTriggerEnter2D(Collider2D other) { if (!WasGoal) { if (other.tag == "AiGoal") { ScoreScriptInstance.Increment(ScoreScript.Score.PlayerScore); WasGoal = true; audioManager.PlayGoal(); StartCoroutine(ResetPuck(false)); } else if (other.tag == "PlayerGoal") { ScoreScriptInstance.Increment(ScoreScript.Score.AiScore); WasGoal = true; audioManager.PlayGoal(); StartCoroutine(ResetPuck(true)); } } } private void OnCollisionEnter2D(Collision2D collision) { audioManager.PlayPuckCollision(); } private IEnumerator ResetPuck(bool didAiScore) { yield return new WaitForSecondsRealtime(1); WasGoal = false; rb.velocity = rb.position = new Vector2(0, 0); if (didAiScore) rb.position = new Vector2(0, -1); else rb.position = new Vector2(0, 1); } private void FixedUpdate() { rb.velocity = Vector2.ClampMagnitude(rb.velocity, MaxSpeed); } }
AudioManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioManager : MonoBehaviour { public AudioClip PuckCollision; public AudioClip Goal; private AudioSource audioSource; private void Start() { audioSource = GetComponent<AudioSource>(); } public void PlayPuckCollision() { audioSource.PlayOneShot(PuckCollision); } public void PlayGoal() { audioSource.PlayOneShot(Goal); } }
is there somewhere to download the SFX files? I’ve been looking for a website to find some free ones but I’m not having any luck. if theres no download for the ones you use, can you recommend a good site to find free SFX downloads? I’m not completely opposed to registering and paying for a sound website, I’m just hoping that theres an alternative to that while I watch youtube tutorials like yours and learn coding!