LinearAudioPitch.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Changes the pitch of this audio source based on a linear mapping
  4. // and a curve
  5. //
  6. //=============================================================================
  7. using UnityEngine;
  8. using System.Collections;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class LinearAudioPitch : MonoBehaviour
  13. {
  14. public LinearMapping linearMapping;
  15. public AnimationCurve pitchCurve;
  16. public float minPitch;
  17. public float maxPitch;
  18. public bool applyContinuously = true;
  19. private AudioSource audioSource;
  20. //-------------------------------------------------
  21. void Awake()
  22. {
  23. if ( audioSource == null )
  24. {
  25. audioSource = GetComponent<AudioSource>();
  26. }
  27. if ( linearMapping == null )
  28. {
  29. linearMapping = GetComponent<LinearMapping>();
  30. }
  31. }
  32. //-------------------------------------------------
  33. void Update()
  34. {
  35. if ( applyContinuously )
  36. {
  37. Apply();
  38. }
  39. }
  40. //-------------------------------------------------
  41. private void Apply()
  42. {
  43. float y = pitchCurve.Evaluate( linearMapping.value );
  44. audioSource.pitch = Mathf.Lerp( minPitch, maxPitch, y );
  45. }
  46. }
  47. }