#Trigger at end of AudioClip

1 messages · Page 1 of 1 (latest)

autumn igloo
#

Let's take it here

cursive sand
#

oh wow

#

awesome

#

ok so i don't have long, not trynna be rude but i gtg soon

autumn igloo
#

just need to boot up my IDE while explaining some sutff

#

how long?

cursive sand
#

not very soon, but like 15 mins

autumn igloo
#

Alright

#

have you made an FPS counter?
Do you know how frames work?

cursive sand
#

i know what a frame is, and i know what fps is, but idk about it in unity

autumn igloo
#

Update() runs every visual frame
FixedUpdate() runs every physics tick or Time.fixedDeltaTime (which is a fixed number)

cursive sand
#

yeah

#

i assume i need to know how many ticks the clip takes

#

or somin like that?

autumn igloo
#

Sort of. We can't trigger it instantaneously.
But we can catch it on the matching current or next frame.

#

By using a Timer

cursive sand
#

ok

#

so how do i do that

#

btw i don't need it instantaneously

autumn igloo
#
using UnityEngine;

public class DeltaTimeCounter : MonoBehaviour
{
    private float frequency = 1.0f;
    private float timer;

    private void Update()
    {
        timer += Time.deltaTime;

        if (timer >= frequency)
        {
            timer -= frequency;
            MyFunction();
        }
    }

    private void MyFunction()
    {
        // Hello, World!
    }
}
#

That is a simple timer.
Some times better to use than Coroutines.

cursive sand
#

ok how do i implement

autumn igloo
#

Do you understand how it works?

cursive sand
#

kinda but i will look into it further when i am not on a clock

autumn igloo
#

In this case, it is repeating. We don't need that.

cursive sand
#

i see

autumn igloo
#

kk. I will have an example for you. Just trying not to make this a hand-out.

cursive sand
#

for sure

#

i get it

autumn igloo
#

trying to be fast merging the script

#

playSound.clip.length

#

AudioSource / AudioClip

cursive sand
#

brb, also gtg in like 5 mins so srry

autumn igloo
#

it's fine, you can read this later anyway

#

Since the timer is not repeating, it has to be encapsulated by an if-statement

#

think I have it now

#
using UnityEngine;
using UnityEngine.SceneManagement;

public class AudioClipTriggerScript : MonoBehaviour
{
    public AudioSource playSound;
    private bool _triggered;

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log("hit detected");
            _triggered = true;
            playSound.Play();
        }
    }

    private float duration;
    private float timer;

    private void Awake()
    {
        duration = playSound.clip.length;
    }

    private void Update()
    {
        if (_triggered)
        {
            timer += Time.deltaTime;

            if (timer >= duration)
            {
                timer = 0f;
                _triggered = false;
                MyFunction();
            } 
        }
    }

    private void MyFunction()
    {
        SceneManager.LoadScene("menuofthought");
    }
}
#

OnTriggerEnter2D flips _triggered to true, which starts the timer in Update
while the sound starts playing

#

Ah, final piece missing

#

Fixed

#

Explanation:

cursive sand
#

ok turns out i have more time than expected

#

so gimme the full explanation

#

also i just added it and it doesn't work so idk what's going on there

#

these issues are back at it again

autumn igloo
#

OnTriggerEnter2D flips the bool, which starts the timer in Update

In order to get the audio clip length (duration), one has to reference the AudioSource in Awake(),
which is the first MonoBehaviour call to run during a frame, when a game object has been loaded into the scene.

When the timer has started, it will add the value of deltaTime to the timer holding variable.
Once that value is equal to or greater than the duration of the audio clip,
the timer will execute the function, while resetting itself.

cursive sand
#

hmm

#

i see

#

i get that about as much as i will get in the next year

#

im also tired so that is a disadvantage. but i get some of it

#

so how do i fix the warnings

autumn igloo
#

What precisely did you do?

cursive sand
#

wait

#

nevermind, sorry

#

hell yeah

#

tysm!

#

is there a way to make it so that an image pops up as the sounds plays but before it changes scene

autumn igloo
#

You're welcome.
Try to understand the code.
The basic timer example I first posted is worth saving as a template.
You could easily modify it to be an FPS counter.

autumn igloo
cursive sand
#

ok that makes sense

#

but how do i do that?

autumn igloo
#

Which Unity Editor version are you using?

cursive sand
#

that is a great question

autumn igloo
#

in the Title bar of your Unity Editor window

cursive sand
#

ok hang on

#

2019.4.20

autumn igloo
#

Ok. That's a bit old today.
The most stable Long Term Support version is 2020.3 LTS

#

2021 is also LTS, but not as stable as 2020

#

2022 is Tech Stream, which is for experienced developers to test out new features

#

One major difference between them, is the C# version they support

cursive sand
#

and can i make the image change a bunch of times

autumn igloo
#

Anyway - not important right now, but it's a good idea to use 2020 for future projects

cursive sand
#

am i able to swap between 2 images real fast

autumn igloo
autumn igloo
cursive sand
#

how do i loop in c# again?

#

im a python pleb so i forget which is which

autumn igloo
#

in Visual Studio, type for and Tab twice

#

replace the broken Length variable with your array.Length or list.Count

cursive sand
#

can i get an example of a loop?

autumn igloo
#

Best you google one

cursive sand
#

alright cool

autumn igloo
#

Google will answer a lot of good questions quite well.
It's kind of insane how easy it is to learn these days, compared to ten years ago.

cursive sand
#

well i gtg fr now so any other bits of advice/examples/website links just send them here for me to see when i get back if you wouldn't mind

#

tysm for your help!

autumn igloo
#

The help kind of ends here. I have limited time to set aside for these things, but you should learn to use the server and the channels, and learn to listen to the people who help others, because they are quite skilled at helping new people grasp the basics so they can move on to advanced.

What I gave you here is a bit rare, and mostly because I think new people some times just need to get results.
Once you learn to modify the Timer example to make an FPS counter and to swap out images, then that is what will make this session a success.

Good luck and stuff.