Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Lever : MonoBehaviour
- {
- // amount we want to move the lever by
- public Vector3 moveOffset = new Vector3(0f, -.5f, 0f);
- // default tween settings for moving down
- public float tweenTimeDown = .5f;
- public Tween.EaseType easeTypeDown = Tween.EaseType.BackOut;
- public float waitWhileDownTime = .1f;
- // default tween settings for moving up
- public float tweenTimeUp = .5f;
- public Tween.EaseType easeTypeUp = Tween.EaseType.BackIn;
- public bool stayDown = false;
- // is the lever pulled down?
- bool isDown;
- // stores the original local position of the lever so we can move back to it
- Vector3 originalLocalPosition;
- void Awake()
- {
- // store original local position so we can move back to it
- originalLocalPosition = transform.localPosition;
- }
- void OnMouseDown()
- {
- // if no tweens are happening right now
- if (Tween.Count(gameObject) == 0)
- {
- // if we're not in stayDown mode or we're up, let the user click
- if (!stayDown || !isDown)
- {
- Toggle();
- }
- }
- }
- void Toggle()
- {
- Tween.Stop(gameObject);
- if (isDown)
- {
- // if the lever is down, move it back up
- Tween.MoveTo(gameObject, originalLocalPosition, tweenTimeUp, easeTypeUp, true);
- Invoke("BackUp", tweenTimeUp);
- }
- else
- {
- // if the lever is up, pull it down
- Tween.MoveTo(gameObject, originalLocalPosition + moveOffset, tweenTimeDown, easeTypeDown, true);
- Invoke("PulledDown", tweenTimeDown);
- }
- }
- void BackUp()
- {
- // do whatever needs to happen when the lever is reset to its original position
- isDown = false;
- }
- void PulledDown()
- {
- // do whatever needs to happen when the lever is pulled down here
- isDown = true;
- if (!stayDown)
- {
- // wait a bit before toggling back up
- Invoke("Toggle", waitWhileDownTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement