LinearAnimator.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Animator whose speed is set based on a linear mapping
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class LinearAnimator : MonoBehaviour
  12. {
  13. public LinearMapping linearMapping;
  14. public Animator animator;
  15. private float currentLinearMapping = float.NaN;
  16. private int framesUnchanged = 0;
  17. //-------------------------------------------------
  18. void Awake()
  19. {
  20. if ( animator == null )
  21. {
  22. animator = GetComponent<Animator>();
  23. }
  24. animator.speed = 0.0f;
  25. if ( linearMapping == null )
  26. {
  27. linearMapping = GetComponent<LinearMapping>();
  28. }
  29. }
  30. //-------------------------------------------------
  31. void Update()
  32. {
  33. if ( currentLinearMapping != linearMapping.value )
  34. {
  35. currentLinearMapping = linearMapping.value;
  36. animator.enabled = true;
  37. animator.Play( 0, 0, currentLinearMapping );
  38. framesUnchanged = 0;
  39. }
  40. else
  41. {
  42. framesUnchanged++;
  43. if ( framesUnchanged > 2 )
  44. {
  45. animator.enabled = false;
  46. }
  47. }
  48. }
  49. }
  50. }