Advertisement
infinite_ammo

Lever.cs

Aug 23rd, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Lever : MonoBehaviour
  5. {
  6.     // amount we want to move the lever by
  7.     public Vector3 moveOffset = new Vector3(0f, -.5f, 0f);
  8.  
  9.     // default tween settings for moving down
  10.     public float tweenTimeDown = .5f;
  11.     public Tween.EaseType easeTypeDown = Tween.EaseType.BackOut;
  12.  
  13.     public float waitWhileDownTime = .1f;
  14.  
  15.     // default tween settings for moving up
  16.     public float tweenTimeUp = .5f;
  17.     public Tween.EaseType easeTypeUp = Tween.EaseType.BackIn;
  18.  
  19.     public bool stayDown = false;
  20.  
  21.     // is the lever pulled down?
  22.     bool isDown;
  23.     // stores the original local position of the lever so we can move back to it
  24.     Vector3 originalLocalPosition;
  25.  
  26.     void Awake()
  27.     {
  28.         // store original local position so we can move back to it
  29.         originalLocalPosition = transform.localPosition;
  30.     }
  31.  
  32.     void OnMouseDown()
  33.     {
  34.         // if no tweens are happening right now
  35.         if (Tween.Count(gameObject) == 0)
  36.         {
  37.             // if we're not in stayDown mode or we're up, let the user click
  38.             if (!stayDown || !isDown)
  39.             {
  40.                 Toggle();
  41.             }
  42.         }
  43.     }
  44.  
  45.     void Toggle()
  46.     {
  47.         Tween.Stop(gameObject);
  48.         if (isDown)
  49.         {
  50.             // if the lever is down, move it back up
  51.             Tween.MoveTo(gameObject, originalLocalPosition, tweenTimeUp, easeTypeUp, true);
  52.             Invoke("BackUp", tweenTimeUp);
  53.         }
  54.         else
  55.         {
  56.             // if the lever is up, pull it down
  57.             Tween.MoveTo(gameObject, originalLocalPosition + moveOffset, tweenTimeDown, easeTypeDown, true);
  58.             Invoke("PulledDown", tweenTimeDown);
  59.         }
  60.     }
  61.  
  62.     void BackUp()
  63.     {
  64.         // do whatever needs to happen when the lever is reset to its original position
  65.         isDown = false;
  66.     }
  67.  
  68.     void PulledDown()
  69.     {
  70.         // do whatever needs to happen when the lever is pulled down here
  71.         isDown = true;
  72.  
  73.         if (!stayDown)
  74.         {
  75.             // wait a bit before toggling back up
  76.             Invoke("Toggle", waitWhileDownTime);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement