#Trigger at end of AudioClip
1 messages · Page 1 of 1 (latest)
not very soon, but like 15 mins
i know what a frame is, and i know what fps is, but idk about it in unity
Update() runs every visual frame
FixedUpdate() runs every physics tick or Time.fixedDeltaTime (which is a fixed number)
Sort of. We can't trigger it instantaneously.
But we can catch it on the matching current or next frame.
By using a Timer
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.
ok how do i implement
Do you understand how it works?
kinda but i will look into it further when i am not on a clock
In this case, it is repeating. We don't need that.
i see
kk. I will have an example for you. Just trying not to make this a hand-out.
trying to be fast merging the script
playSound.clip.length
AudioSource / AudioClip
brb, also gtg in like 5 mins so srry
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:
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
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.
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
What precisely did you do?
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
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.
Yes, if you have a Canvas with UI stuff, it can be enabled == true
Which Unity Editor version are you using?
that is a great question
in the Title bar of your Unity Editor window
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
You won't notice much with that, especially when following C# Unity tutorials.
But newer versions of C# have more neat features, and new tools that open up new possibilities.
https://docs.unity3d.com/2020.3/Documentation/Manual/CSharpCompiler.html
and can i make the image change a bunch of times
Anyway - not important right now, but it's a good idea to use 2020 for future projects
am i able to swap between 2 images real fast
yes, by modifying the Timer example to swap out the sprite of the SpriteRenderer or Image component
Yes
in Visual Studio, type for and Tab twice
replace the broken Length variable with your array.Length or list.Count
can i get an example of a loop?
Best you google one
alright cool
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.
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!
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.