Advertisement
Guest User

AudioController.cs

a guest
Sep 21st, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AudioController : MonoBehaviour {
  5.  
  6.     public AudioSource[] sound;
  7.     public AudioSource steps;
  8.     public AudioSource guns;
  9.     public AudioClip[] concrete;
  10.     public AudioClip[] dirt;
  11.     public AudioClip gun1;
  12.     public AudioClip reloadGun1;
  13.     public AudioClip russianSong;
  14.     private bool isMoving;
  15.     [SerializeField]
  16.     private float stepRate;
  17.     private string floortag;
  18.     private bool step;
  19.     private bool playingStep;
  20.  
  21.     public AudioSync audioSync;
  22.     public TauntController tauntController;
  23.     public GravityFPSWalker controller;
  24.     public AudioListener playerAudioListener;
  25.  
  26.     void Start()
  27.     {
  28.         tauntController = GetComponent<TauntController>();
  29.         controller = GetComponent<GravityFPSWalker>();
  30.         sound = GetComponents<AudioSource>();
  31.         playerAudioListener = GetComponent<AudioListener>();
  32.         audioSync = GetComponent<AudioSync>();
  33.         steps = sound[0];
  34.         guns = sound[1];
  35.         guns.clip = gun1;
  36.         isMoving = false;
  37.     }
  38.  
  39.     void Update()
  40.     {
  41.         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
  42.         {
  43.             isMoving = true;
  44.         }
  45.         else
  46.         {
  47.             isMoving = false;
  48.         }
  49.     }
  50.  
  51.     void FixedUpdate()
  52.     {
  53.         RaycastHit hit;
  54.         if (controller.grounded == true)
  55.         {
  56.             CalculateSound();
  57.             if (Physics.Raycast(transform.position, Vector3.down, out hit))
  58.             {
  59.                 floortag = hit.collider.gameObject.tag;
  60.             }
  61.         }
  62.     }
  63.  
  64.     //Determines whether to play footsteps or not
  65.     void CalculateSound()
  66.     {
  67.         if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical"))
  68.         {
  69.             if (tauntController.russianDance == false && playingStep == false)
  70.             {
  71.                 InvokeRepeating("playStep", 0f, stepRate);
  72.             }
  73.         }
  74.         if (isMoving == false)
  75.         {
  76.             CancelInvoke("playStep");
  77.             playingStep = false;
  78.         }
  79.     }
  80.  
  81.     //Plays foostep sounds to the local client
  82.     void playStep()
  83.     {
  84.         if (controller.grounded == true)
  85.         {
  86.             playingStep = true;
  87.             if (floortag == "dirt")
  88.             {
  89.                 steps.clip = dirt[Random.Range(0, dirt.Length)];
  90.             }
  91.             if (floortag == "concrete")
  92.             {
  93.                 steps.clip = concrete[Random.Range(0, concrete.Length)];
  94.             }
  95.             Debug.Log("Played " + floortag);
  96.             steps.Play();
  97.         }
  98.     }
  99.  
  100.     public void playTauntSong()
  101.     {
  102.  
  103.         if (tauntController.russianDance == true)
  104.         {
  105.             playerAudioListener.enabled = true;
  106.             steps.clip = russianSong;
  107.             steps.loop = true;
  108.             steps.Play();
  109.             Debug.Log("Playing Kazotsky Kick");
  110.         }
  111.  
  112.     }
  113.  
  114.     public void stopTauntSong()
  115.     {
  116.         playerAudioListener.enabled = false;
  117.         steps.Stop();
  118.         steps.loop = false;
  119.         steps.clip = null;
  120.         Debug.Log("Pausing Taunt Song");
  121.     }
  122.  
  123.     public void playGunShots()
  124.     {
  125.         guns.PlayOneShot(gun1);
  126.         Debug.Log("Bang!");
  127.     }
  128.  
  129.     public void playReload()
  130.     {
  131.         guns.PlayOneShot(reloadGun1);
  132.         Debug.Log("Played Reloaded");
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement