Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace SpartanFist
- {
- [AddComponentMenu("Interactions/Switches/Floor Switch")]
- public class DepressionSwitch : Switch
- {
- [Header("References"), Tooltip("(optional) this is the mesh we're depressing down or popping back up with switch state")]
- public GameObject switchMeshObj;
- protected float curSwitchBlendTime = 0.0f;
- protected float baseButtonY = 0.0f;
- /* Magic Numbers */
- protected const float SwitchDepressionAmount = 0.4f;
- protected const float SwitchMoveTime = 0.5f;
- /* Standard Functions */
- public override void Awake()
- {
- base.Awake();
- if (switchMeshObj != null)
- {
- Vector3 curSwitchPos = switchMeshObj.transform.localPosition;
- baseButtonY = curSwitchPos.y;
- if (defaultSwitchState) { curSwitchPos.y = baseButtonY - SwitchDepressionAmount; }
- else { curSwitchPos.y = baseButtonY; }
- switchMeshObj.transform.localPosition = curSwitchPos;
- }
- }
- public override void Update()
- {
- base.Update();
- if (curSwitchBlendTime > 0.0f)
- {
- float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
- curSwitchBlendTime -= deltaTime;
- if (curSwitchBlendTime <= 0.0f)
- {
- curSwitchBlendTime = 0.0f;
- }
- if (switchMeshObj != null)
- {
- float tVal = curSwitchBlendTime / SwitchMoveTime;
- Vector3 curSwitchPos = switchMeshObj.transform.localPosition;
- if (SwitchState)
- {
- // (for switch on, we want to attack into the state faster)
- tVal *= tVal;
- tVal += tVal;
- tVal = 1.0f - tVal;
- curSwitchPos.y = baseButtonY + Mathf.Lerp(0.0f, -SwitchDepressionAmount, tVal);
- }
- else
- {
- // (for switch off, we want to ease out of the state slowly)
- tVal = 1.0f - tVal;
- tVal *= tVal;
- curSwitchPos.y = baseButtonY + Mathf.Lerp(-SwitchDepressionAmount, 0.0f, tVal);
- }
- switchMeshObj.transform.localPosition = curSwitchPos;
- }
- }
- }
- /// <summary>
- /// This is someone tapping their foot down on the switch
- /// </summary>
- protected override void SwitchOn()
- {
- base.SwitchOn();
- curSwitchBlendTime = SwitchMoveTime;
- }
- /// <summary>
- /// This is someone walking off the switch
- /// </summary>
- protected override void SwitchOff()
- {
- base.SwitchOff();
- curSwitchBlendTime = SwitchMoveTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment