using System.Collections; using System.Collections.Generic; using UnityEngine; public class CurtainBehaviour : MonoBehaviour { [SerializeField] private Cloth curtainCloth; public float fadeSpeed = 2.0f; // The speed at which to fade towards the external acceleration public float fadeDelay = 1.0f; // Optional delay before starting the fading process [Tooltip("BECAREFUL if used at start then there will be no forces applied if the dev has (ApplyAccelerationToCurtainAtStart) bool TRUE as well")] public bool ZeroAccelerationToCurtainsAtStart; public bool ApplyAccelerationToCurtainAtStart; public Vector3 targetExternalAccelerationToCurtain; public Vector3 targetRandomAccelerationToCurtain; public float lerpToTarget = 0; Coroutine accelerationFadeIn; Coroutine accelerationFadeOut; //private void Awake() //{ // curtainCloth.externalAcceleration = new Vector3(curtainCloth.externalAcceleration.x, 1000f, curtainCloth.externalAcceleration.z); //} // Start is called before the first frame update void Start() { if (ZeroAccelerationToCurtainsAtStart) { curtainCloth.externalAcceleration = Vector3.zero; curtainCloth.randomAcceleration = Vector3.zero; } StartCoroutine(AtStartOfGame()); if (ApplyAccelerationToCurtainAtStart) { StartRandomAccelerationToCurtain(); } } public void FixCurtains() { StartCoroutine(AtStartOfGame()); } IEnumerator AtStartOfGame() { curtainCloth.externalAcceleration = new Vector3(curtainCloth.externalAcceleration.x, 3000f, curtainCloth.externalAcceleration.z); print("Skato kourtines"); yield return new WaitForSeconds(0.1f); curtainCloth.externalAcceleration = new Vector3(curtainCloth.externalAcceleration.x, 0f, curtainCloth.externalAcceleration.z); } public void StartRandomAccelerationToCurtain() { print("Started acceleration to curtain"); accelerationFadeIn = StartCoroutine(AccelerationToCurtainFadeIn()); if (accelerationFadeOut != null) { StopCoroutine(accelerationFadeOut); accelerationFadeOut = null; } } public void StopRandomAccelerationToCurtain() { print("Stopping acceleration to curtain"); accelerationFadeOut = StartCoroutine(AccelerationToCurtainFadeOut()); if (accelerationFadeIn != null) { StopCoroutine(accelerationFadeIn); accelerationFadeOut = null; } } IEnumerator AccelerationToCurtainFadeIn() { lerpToTarget = 0f; float transitionTime = 1; yield return new WaitForSeconds(0f); while (lerpToTarget <= transitionTime) { lerpToTarget += Time.deltaTime; float normalizedTime = Mathf.Clamp01(lerpToTarget / transitionTime); // Update the current target position towards the externalAcceleration curtainCloth.externalAcceleration = Vector3.Lerp(curtainCloth.externalAcceleration, targetExternalAccelerationToCurtain, normalizedTime); curtainCloth.randomAcceleration = Vector3.Lerp(curtainCloth.externalAcceleration, targetRandomAccelerationToCurtain, normalizedTime); yield return null; } // Ensure the final value is exactly the externalAcceleration to avoid slight discrepancies curtainCloth.externalAcceleration = targetExternalAccelerationToCurtain; curtainCloth.randomAcceleration = targetRandomAccelerationToCurtain; } IEnumerator AccelerationToCurtainFadeOut() { lerpToTarget = 0f; float transitionTime = 1; yield return new WaitForSeconds(0f); while (lerpToTarget <= transitionTime) { lerpToTarget += Time.deltaTime; float normalizedTime = Mathf.Clamp01(lerpToTarget / transitionTime); // Update the current target position towards the externalAcceleration curtainCloth.externalAcceleration = Vector3.Lerp(curtainCloth.externalAcceleration, Vector3.zero, transitionTime); curtainCloth.randomAcceleration = Vector3.Lerp(curtainCloth.externalAcceleration, Vector3.zero, transitionTime); yield return null; } // Ensure the final value is exactly the externalAcceleration to avoid slight discrepancies curtainCloth.externalAcceleration = Vector3.zero; curtainCloth.randomAcceleration = Vector3.zero; } }