Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio How do I stop audio from constantly updating and stacking?

Discussion in 'Audio & Video' started by quaileqqs, Apr 29, 2020.

  1. quaileqqs

    quaileqqs

    Joined:
    Apr 21, 2020
    Posts:
    1
    I'm trying to play an Engine sound for a tank as it's driving. The way the code is written, the script constantly updates with info but my Wwise event keeps playing and stacking as it updates. Here is the portion of code.


    private void Update ()
    {
    // Store the value of both input axes.
    m_MovementInputValue = Input.GetAxis (m_MovementAxisName);
    m_TurnInputValue = Input.GetAxis (m_TurnAxisName);

    EngineAudio ();
    }


    private void EngineAudio()
    {
    // If there is no input (the tank is stationary)...
    if (Mathf.Abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
    {
    AkSoundEngine.PostEvent("MovementIdle", gameObject);



    // ... and if the audio source is currently playing the driving clip...

    else
    {
    AkSoundEngine.PostEvent("MovementDriving", gameObject);
    }

    }
     
  2. Monetai

    Monetai

    Joined:
    Sep 18, 2015
    Posts:
    4
    Hello!

    I think you should not use PostEvent in this case but rather use several RTPC linked to value of your event in wwise.
    Something more like:


    Code (CSharp):
    1.     void Start()
    2.     {
    3.         AkSoundEngine.PostEvent("EngineOn", gameObject);
    4.     }
    5.  
    6.     public void Update()
    7.     {
    8.         float forwardSpeed = GetNormalizedForwardSpeed(); //return forward speed between 0 and 1
    9.         float turnValue = GetNormalizedTurnValue(); //return turn rate between 0 and 1
    10.  
    11.         AkSoundEngine.SetRTPCValue("ForwardSpeed", forwardSpeed, gameObject);
    12.         AkSoundEngine.SetRTPCValue("TurnValue", turnValue, gameObject);
    13.     }
    Ps: Don't hesitate use to the code balise when posting code like this =)