using System.Collections; using System.Collections.Generic; using UnityEngine; using Mirror; [RequireComponent(typeof(AudioSource))] public class SciFiDoor : NetworkBehaviour, IInteractable { [SyncVar(hook = nameof(OnStateChanged))] public bool state = false; public AudioClip openClip; public AudioClip closeClip; public float duration = 2f, offset; public AnimationCurve oCurva; public List doorList; private float _posX, _originX; private float _timeElapsed; private AudioSource _audioSource; void Start() { _originX = doorList[0].localPosition.x; _audioSource = GetComponent(); } public void Interact() { CmdInteract(); } [Command(requiresAuthority = false)] private void CmdInteract(NetworkConnectionToClient sender = null) { state = !state; } void OnStateChanged(bool _, bool value) { _audioSource.clip = value ? openClip : closeClip; _audioSource.pitch = duration / _audioSource.clip.length; _audioSource.Play(); } void Update() { _timeElapsed += Time.deltaTime * (state ? 1 : -1); _timeElapsed = Mathf.Clamp(_timeElapsed, 0, duration); float progress = _timeElapsed / duration; if (progress >= 1f) { _audioSource.Stop(); return; } var t = oCurva.Evaluate(progress); _posX = Mathf.Lerp(_originX, _originX + offset, t); // _posX = Mathf.Lerp(_originX, _originX + offset, Mathf.SmoothStep(0, 1, progress)); if (_audioSource.clip) { _audioSource.timeSamples = Mathf.FloorToInt(Mathf.Abs(_audioSource.clip.samples * t)); } for (int i = 1; i <= doorList.Count; i++) { float side = i % 2 == 0 ? -1 : 1; var pos = doorList[i - 1].localPosition; pos.x = _posX * side; doorList[i - 1].localPosition = pos; } } }