Night.owl
Night.owl

Reputation: 121

Do something when the audio source has been playing for 2 mins

I've got a script working that blends one skybox into another. I need to write some code so that this blend animation happens when the audiosource is at 2 minutes.

How can I do this? I've read about AudioSource.timeSamples, but I don't really understand how I know when 2 minutes is reached. Should I use some other method?

Upvotes: 1

Views: 148

Answers (2)

Night.owl
Night.owl

Reputation: 121

Here is the working script:

using UnityEngine;
using System.Collections;

public class animateSkyBox : MonoBehaviour {

public Skybox sky;
public Material skyboxmaterial; 
float tParam = 0f;
float valToBeLerped = 0f;
float speed = 0.1f; 



    void Start()
  {
        skyboxmaterial.SetFloat("_Blend", 0);
  Debug.Log("Audio begins now.....");
  Invoke("TwoMinutesHasPassed", 10f);
  }

void TwoMinutesHasPassed()
  {
  Debug.Log("two minutes has passed");
  Debug.Log("now i will fade the background");
  StartCoroutine("FadeNow");
  }

private IEnumerator FadeNow()
  {
  tParam = 0f;
  while (tParam < 1)
    {
    tParam +=  Time.deltaTime;  
    valToBeLerped = Mathf.Lerp(0, 1, tParam);
            skyboxmaterial.SetFloat("_Blend", valToBeLerped);
            yield return null;
    Debug.Log("valToBeLerped is " + valToBeLerped.ToString("f4"));

    }
  //skyboxmaterial.SetFloat("_Blend", valToBeLerped);
  Debug.Log("fade is done.");
  yield break;
  }

  }

Upvotes: 0

Fattie
Fattie

Reputation: 12646

void Start()
  {
  Debug.Log("Audio begins now.....");
  Invoke("TwoMinutesHasPassed", 120f);
  }

void TwoMinutesHasPassed()
  {
  Debug.Log("two minutes has passed");
  Debug.Log("now i will fade the background");
  StartCoroutine("FadeNow");
  }

private IEnumerator FadeNow()
  {
  tParam = 0f;
  while (tParam < 1)
    {
    tParam += Time.deltaTime * speed;  
    valToBeLerped = Mathf.Lerp(0, 1, tParam);
    Debug.Log("valToBeLerped is " + valToBeLerped.ToString("f4"));
    yield return null;
    }
  skyboxmaterial.SetFloat("_Blend", valToBeLerped);
  Debug.Log("fade is done.");
  }

Upvotes: 2

Related Questions