I want when play’s clicking to the game, the time will get slow and also play the slow motion sound effect but the problem is the slow motion sound effect also get affected by the time.timescale, is there any way to fix it?
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0))
{
Time.timeScale = timeSlow;
slowMo.Play();
}
Can you rephrase the problem? Do you want the audio to change pitch?
I tried to change the pitch but i think it is not the problem, the issue is when i set the time.TimeScale to 0.2f, it also makes the sound effect play at like 0.2 the normal speed
Does it? That’s not how I understand AudioSource to work.
I just made a script with an AudioSource to test this. I changed the timescale and played an AudioClip, and the audio wasn’t affected by timescale.
flashframe:
Does it? That’s not how I understand AudioSource to work.
I just made a script with an AudioSource to test this. I changed the timescale and played an AudioClip, and the audio wasn’t affected by timescale.
Can you show me the script?, i don’t really understand why.
This is all I did:
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
Debug.Log(Time.timeScale);
Time.timeScale = 0.2f;
GetComponent<AudioSource>().Play();
}
private void Update()
{
Time.timeScale += Time.unscaledDeltaTime / 10f;
Debug.Log(Time.timeScale);
}
}
The audio doesn’t change pitch / speed unless I also set the AudioSource.pitch to the same value as the timeScale.
You should try it in the update function, this is my code, when player’s holding the left mouse button, the timescale is set to 0.3f (timeSlow) to make the slow motion and play the slow motion sound effect.
void Update()
{
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0))
{
Time.timeScale = timeSlow + Time.deltaTime * 10;
if (Time.timeScale > 1)
{
Time.timeScale = 1;
}
Time.fixedDeltaTime = timeSlow * Time.deltaTime;
GetComponent<AudioSource>().Play();
}
Well, that’s because you are calling Play() in update. You should only call Play once to start your sound playing
Sorry, misread the script. I realise you aren’t doing that.
I copied your code to my test, and get the same result - no change in the audio
Is there something else in your project that is in control of the audio pitch? Are you using an Audio Mixer?
In fact, you are calling AudioSource.Play every frame in that code.
Shouldn’t it be something like this?
if(Input.GetMouseButtonDown(0))
{
GetComponent<AudioSource>().Play();
}
if (Input.GetMouseButton(0))
{
Time.timeScale = timeSlow + Time.deltaTime;
Debug.Log(Time.timeScale);
if (Time.timeScale > 1)
{
Time.timeScale = 1;
}
Time.fixedDeltaTime = timeSlow * Time.deltaTime;
}