Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

  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. }