Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AudioFootSteps : MonoBehaviour
  6. {
  7. PlayerControllerNew pl;
  8. bool startedSound0;
  9. bool startedSound1;
  10.  
  11. [SerializeField] private float fadeTime = 2f;
  12. [SerializeField] private float crossfadeTime = 2f;
  13.  
  14.  
  15. [SerializeField] private AudioSource audioSource0;
  16. [SerializeField] private AudioSource audioSource1;
  17.  
  18. private bool fromStill;
  19.  
  20.  
  21. private void Awake()
  22. {
  23. fromStill = false;
  24. audioSource0.volume = 0;
  25. audioSource1.volume = 0;
  26. }
  27.  
  28. void Start()
  29. {
  30. pl = GetComponent<PlayerControllerNew>();
  31. StartCoroutine(Fade(audioSource1, 1));
  32. }
  33.  
  34. // Update is called once per frame
  35. void Update()
  36. {
  37.  
  38. if (pl.currentVelocity == 0f)
  39. {
  40. Debug.Log("Still");
  41. fromStill = true;
  42. audioSource0.volume = 0;
  43. audioSource1.volume = 0;
  44. }
  45.  
  46. if (pl.isRunning == true && pl.currentVelocity != 0f)
  47. {
  48. Debug.Log("RUNNING");
  49. if (fromStill == true)
  50. {
  51. audioSource1.volume = 100;
  52. fromStill = false;
  53. }
  54. else
  55. {
  56. StartCoroutine(DoCrossfade(audioSource0, audioSource1));
  57. }
  58. }
  59.  
  60. if (pl.isWalking == true && pl.currentVelocity != 0f)
  61. {
  62. Debug.Log("WALKING");
  63. if (fromStill == true)
  64. {
  65. audioSource0.volume = 100;
  66. fromStill = false;
  67. }
  68. else
  69. {
  70. StartCoroutine(DoCrossfade(audioSource1, audioSource0));
  71. }
  72. }
  73.  
  74. }
  75.  
  76.  
  77. private IEnumerator Fade(AudioSource source, int direction)
  78. {
  79. int result = direction == 1 ? 1 : 0;
  80. while (source.volume < result)
  81. {
  82. source.volume += Time.deltaTime * direction / fadeTime;
  83. yield return null;
  84. }
  85. }
  86.  
  87. private IEnumerator DoCrossfade(AudioSource a, AudioSource b)
  88. {
  89. while (a.volume > 0)
  90. {
  91. a.volume -= Time.deltaTime * 1 / crossfadeTime;
  92. b.volume += Time.deltaTime * 1 / crossfadeTime;
  93. yield return null;
  94. }
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement