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