Sound is not consistent

I really hope I can explain this situation so that it makes sense. I’m using the AudioClip SFX - Main engine thrust that Rick created. From the very beginning, when pressing spacebar for thrust, the sound was not the “whooshing” sound Rick created but more of a "digital’ noise. I dealt with it because although it did not sound correct it did appear to be working properly. That is unless I hit an object with the spacebar held down. At that point the sound changes to the “whooshing” sound that Rick created for however long my “ReloadLevel” delay is set. Once my level resets the sound goes back to the “digital” noise. Curious to why I can hear the correct “Rick Sound” but only after hitting an object.

It sounds like (hehe) there’s a problem with your code. Please share it. I think your sound is being restarted on every frame for as long as you are holding down the spacebar. When you hit an obstacle, the input is disabled and then the sound gets to complete instead of being restarted the whole time.

Hi Lincoln,

I agree with bixarrio. What you describe sounds like a common problem in this project. Your code probably restarts the AudioSource each frame by calling Play(). The solution is to check whether the AudioSource is playing, and call Play() only if it is not playing. Rick implements this solution at some point.

However, the solution is not difficult. If you want to challenge yourself, take a look at the AudioSource class in the Unity API, and skim the class members. Then you’ll find something that sounds exactly what you need. There is also a code example.

I hope this helped. :slight_smile:


See also:

My apologies for being new to this and I hope I am sharing my code correctly. This is what I have and from what I can tell it looks the same as Ricks but it is possible that I missed something.
I hope that the code comes across in a readable fashion.

using UnityEngine;

public class Movement : MonoBehaviour
{
[SerializeField] float rotationThrust = 1f;
[SerializeField] float mainThrust = 100f;
Rigidbody rb; // Created variable name
AudioSource audioSource; //Created variable name

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>(); //assigned Rigidbody component to rb
    audioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
    ProcessThrust();
    ProcessRotation();
}

void ProcessThrust()
{
    if (Input.GetKey(KeyCode.Space))
    {
        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime); /* added relative force to rb using
        if(!audioSource.isPlaying)                                      Vector3 shorthand*/
        {
            audioSource.Play();
        }
    }
    else
    {
        audioSource.Stop();
    }
}

void ProcessRotation()
{
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(rotationThrust);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-rotationThrust);
    }
}

void ApplyRotation(float rotationThisFrame)
{
    rb.freezeRotation = true; // freezing rotation so we can manually rotate
    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
    rb.freezeRotation = false; // unfreezing rotation so physics system can take over
}

}

Yes, here. The if is commented out, so it’s never checked. The sound plays every frame, so it’s continuously restarted. Your block comment is the problem. If you want it to be on multiple lines, don’t use a block comment

        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime); // added relative force to rb using
        if(!audioSource.isPlaying)                                     // Vector3 shorthand
        {
            audioSource.Play();
        }

I love how when you point it out it seems so obvious lol. Thanks for the help. I really appreciate it.

Many errors are fairly obvious if somebody points the solution out or if you discover it yourself. Even professional programmers sometimes spend hours or days to find a problem just to notice that they misplaced a semicolon. :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms