Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class Gate : MonoBehaviour
  5. {
  6.     [Header("Animation Settings: ")]
  7.     [SerializeField] private float openGateDuration = 3f;
  8.     [SerializeField] private float closeGateDuration = 0.5f;
  9.     [SerializeField] private AnimationCurve openGateCurve;
  10.     [SerializeField] private AnimationCurve closeGateCurve;
  11.     [SerializeField] private Vector3 openGateOffset;
  12.     private Coroutine openGateRoutine;
  13.  
  14.     [Header("Audio: ")]
  15.     [SerializeField] private AudioClip openGateSFX;
  16.     [SerializeField] private AudioClip closeGateSFX;
  17.     [SerializeField] private AudioClip shutGateSFX;
  18.     private AudioSource audioSource;
  19.  
  20.     private Vector3 originalPos;
  21.  
  22.     void Start()
  23.     {
  24.         originalPos = transform.position;
  25.         audioSource = GetComponent<AudioSource>();
  26.         if (audioSource == null) audioSource = gameObject.AddComponent<AudioSource>();
  27.         audioSource.loop = false;
  28.     }
  29.  
  30.     public void OpenGate()
  31.     {
  32.         if (openGateRoutine != null) StopCoroutine(openGateRoutine);
  33.         openGateRoutine = StartCoroutine(IOpenGate());
  34.     }
  35.  
  36.     public void CloseGate()
  37.     {
  38.         if (openGateRoutine != null) StopCoroutine(openGateRoutine);
  39.         openGateRoutine = StartCoroutine(ICloseGate());
  40.     }
  41.  
  42.     IEnumerator IOpenGate()
  43.     {
  44.         Vector3 _startPos = transform.position;
  45.         audioSource.PlayOneShot(openGateSFX);
  46.         float _loopTimer = openGateSFX.length;
  47.  
  48.         float _lerpTime = 0;
  49.         while (_lerpTime < 1)
  50.         {
  51.             _lerpTime += Time.deltaTime / openGateDuration;
  52.             float _lerpKey = openGateCurve.Evaluate(_lerpTime);
  53.  
  54.             _loopTimer -= Time.deltaTime;
  55.             if(_loopTimer <= 0)
  56.             {
  57.                 _loopTimer = openGateSFX.length;
  58.                 audioSource.PlayOneShot(openGateSFX);
  59.             }
  60.  
  61.  
  62.             transform.position = Vector3.Lerp(_startPos, originalPos + openGateOffset, _lerpKey);
  63.  
  64.             yield return null;
  65.         }
  66.  
  67.         yield return null;
  68.     }
  69.  
  70.     IEnumerator ICloseGate()
  71.     {
  72.         Vector3 _startPos = transform.position;
  73.         audioSource.PlayOneShot(closeGateSFX);
  74.         float _loopTimer = openGateSFX.length;
  75.  
  76.         float _lerpTime = 0;
  77.         while (_lerpTime < 1)
  78.         {
  79.             _lerpTime += Time.deltaTime / closeGateDuration;
  80.             float _lerpKey = closeGateCurve.Evaluate(_lerpTime);
  81.  
  82.             _loopTimer -= Time.deltaTime;
  83.             if (_loopTimer <= 0)
  84.             {
  85.                 _loopTimer = openGateSFX.length;
  86.                 audioSource.PlayOneShot(openGateSFX);
  87.             }
  88.  
  89.             transform.position = Vector3.Lerp(_startPos, originalPos, _lerpKey);
  90.  
  91.             yield return null;
  92.         }
  93.  
  94.         audioSource.PlayOneShot(shutGateSFX);
  95.  
  96.         yield return null;
  97.     }
  98.  
  99. }