IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
LinearAnimator.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Animator whose speed is set 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 LinearAnimator : MonoBehaviour
14  {
15  public LinearMapping linearMapping;
16  public Animator animator;
17 
18  private float currentLinearMapping = float.NaN;
19  private int framesUnchanged = 0;
20 
21 
22  //-------------------------------------------------
23  void Awake()
24  {
25  if ( animator == null )
26  {
27  animator = GetComponent<Animator>();
28  }
29 
30  animator.speed = 0.0f;
31 
32  if ( linearMapping == null )
33  {
34  linearMapping = GetComponent<LinearMapping>();
35  }
36  }
37 
38 
39  //-------------------------------------------------
40  void Update()
41  {
42  if ( currentLinearMapping != linearMapping.value )
43  {
44  currentLinearMapping = linearMapping.value;
45  animator.enabled = true;
46  animator.Play( 0, 0, currentLinearMapping );
47  framesUnchanged = 0;
48  }
49  else
50  {
51  framesUnchanged++;
52  if ( framesUnchanged > 2 )
53  {
54  animator.enabled = false;
55  }
56  }
57  }
58  }
59 }