Advertisement
infinite_ammo

Lever.cs

Aug 23rd, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 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 up
  10.     public float tweenTimeUp = .5f;
  11.     public Tween.EaseType easeTypeUp = Tween.EaseType.BackIn;
  12.  
  13.  
  14.     // default tween settings for moving down
  15.     public float tweenTimeDown = .5f;
  16.     public Tween.EaseType easeTypeDown = Tween.EaseType.BackOut;
  17.  
  18.     public bool stayDown = false;
  19.  
  20.     // is the lever pulled down?
  21.     bool isDown;
  22.     // stores the original local position of the lever so we can move back to it
  23.     Vector3 originalLocalPosition;
  24.  
  25.     void Awake()
  26.     {
  27.         // store original local position so we can move back to it
  28.         originalLocalPosition = transform.localPosition;
  29.     }
  30.  
  31.     void OnMouseDown()
  32.     {
  33.         // if no tweens are happening right now
  34.         if (Tween.Count(gameObject) == 0)
  35.         {
  36.             Toggle();
  37.         }
  38.     }
  39.  
  40.     void Toggle()
  41.     {
  42.         Tween.Stop(gameObject);
  43.         if (isDown)
  44.         {
  45.             // if the lever is down, move it back up
  46.             Tween.MoveTo(gameObject, originalLocalPosition, tweenTimeUp, easeTypeUp, true);
  47.             Invoke("BackUp", tweenTimeUp);
  48.         }
  49.         else
  50.         {
  51.             // if the lever is up, pull it down
  52.             Tween.MoveTo(gameObject, originalLocalPosition + moveOffset, tweenTimeDown, easeTypeDown, true);
  53.             Invoke("PulledDown", tweenTimeDown);
  54.         }
  55.     }
  56.  
  57.     void BackUp()
  58.     {
  59.         // do whatever needs to happen when the lever is reset to its original position
  60.         isDown = false;
  61.     }
  62.  
  63.     void PulledDown()
  64.     {
  65.         // do whatever needs to happen when the lever is pulled down here
  66.         isDown = true;
  67.  
  68.         if (!stayDown)
  69.         {
  70.             Toggle();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement