LinearAnimation.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Animation that moves based on a linear mapping
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class LinearAnimation : MonoBehaviour
  12. {
  13. public LinearMapping linearMapping;
  14. public new Animation animation;
  15. private AnimationState animState;
  16. private float animLength;
  17. private float lastValue;
  18. //-------------------------------------------------
  19. void Awake()
  20. {
  21. if ( animation == null )
  22. {
  23. animation = GetComponent<Animation>();
  24. }
  25. if ( linearMapping == null )
  26. {
  27. linearMapping = GetComponent<LinearMapping>();
  28. }
  29. //We're assuming the animation has a single clip, and that's the one we're
  30. //going to scrub with the linear mapping.
  31. animation.playAutomatically = true;
  32. animState = animation[animation.clip.name];
  33. //If the anim state's (i.e. clip's) wrap mode is Once (the default) or ClampForever,
  34. //Unity will automatically stop playing the anim, regardless of subsequent changes
  35. //to animState.time. Thus, we set the wrap mode to PingPong.
  36. animState.wrapMode = WrapMode.PingPong;
  37. animState.speed = 0;
  38. animLength = animState.length;
  39. }
  40. //-------------------------------------------------
  41. void Update()
  42. {
  43. float value = linearMapping.value;
  44. //No need to set the anim if our value hasn't changed.
  45. if ( value != lastValue )
  46. {
  47. animState.time = value / animLength;
  48. }
  49. lastValue = value;
  50. }
  51. }
  52. }