Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AudioFootSteps : MonoBehaviour
- {
- PlayerControllerNew pl;
- bool startedSound0;
- bool startedSound1;
- [SerializeField] private float fadeTime = 2f;
- [SerializeField] private float crossfadeTime = 2f;
- [SerializeField] private AudioSource audioSource0;
- [SerializeField] private AudioSource audioSource1;
- private bool fromStill;
- private void Awake()
- {
- fromStill = false;
- audioSource0.volume = 0;
- audioSource1.volume = 0;
- }
- void Start()
- {
- pl = GetComponent<PlayerControllerNew>();
- StartCoroutine(Fade(audioSource1, 1));
- }
- // Update is called once per frame
- void Update()
- {
- if (pl.currentVelocity == 0f)
- {
- Debug.Log("Still");
- fromStill = true;
- audioSource0.volume = 0;
- audioSource1.volume = 0;
- }
- if (pl.isRunning == true && pl.currentVelocity != 0f)
- {
- Debug.Log("RUNNING");
- if (fromStill == true)
- {
- audioSource1.volume = 100;
- fromStill = false;
- }
- else
- {
- StartCoroutine(DoCrossfade(audioSource0, audioSource1));
- }
- }
- if (pl.isWalking == true && pl.currentVelocity != 0f)
- {
- Debug.Log("WALKING");
- if (fromStill == true)
- {
- audioSource0.volume = 100;
- fromStill = false;
- }
- else
- {
- StartCoroutine(DoCrossfade(audioSource1, audioSource0));
- }
- }
- }
- private IEnumerator Fade(AudioSource source, int direction)
- {
- int result = direction == 1 ? 1 : 0;
- while (source.volume < result)
- {
- source.volume += Time.deltaTime * direction / fadeTime;
- yield return null;
- }
- }
- private IEnumerator DoCrossfade(AudioSource a, AudioSource b)
- {
- while (a.volume > 0)
- {
- a.volume -= Time.deltaTime * 1 / crossfadeTime;
- b.volume += Time.deltaTime * 1 / crossfadeTime;
- yield return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement