Help with Tanks! tutorial project

I want to make a sound be played while tanks are moving and another sound be played when they’re static, but I can’t make it work correctly. I tried a couple of different codes and they all ended up in the same result: When I press W the sound starts but it does not stop when I release the key. Can someone help me with that?

Are you able to show how your FMOD Studio event is set up and the code you have written that plays & controls the event?

I have 2 FMOD Studio events, an idle one and a movement one. They’re both just an audio clip with a loop region

And this is the code that I think is more close to one that actually works and also the one that I wrote in the beginning:


I’ve deleted other codes that didn’t work, but they were basically the same code on the Update function (this one is in the EngineAudio function) and another one using “while” condition instead of “if”

PS: Since I’m a new user I can only use one image. Sorry about that.

I can’t see the rest of the code so I’m not certain about certain variables. Are the m_MovementAudio variables also FMOD events or are those Unity audio clips? In the FMOD events, I can’t see the event instance being released, nor can I see the “clips” stopping.

Are you able to copy and paste this code in its entirety?

The m_MovementAudio variables are Unity audio clips, they were already in the script when I first opened the project.

using UnityEngine;

namespace Complete
{
public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; // Used to identify which tank belongs to which player. This is set by this tank’s manager.
public float m_Speed = 12f; // How fast the tank moves forward and back.
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
public AudioSource m_MovementAudio; // Reference to the audio source used to play engine sounds. NB: different to the shooting audio source.
public AudioClip m_EngineIdling; // Audio to play when the tank isn’t moving.
public AudioClip m_EngineDriving; // Audio to play when the tank is moving.
public float m_PitchRange = 0.2f; // The amount by which the pitch of the engine noises can vary.

    private string m_MovementAxisName;          // The name of the input axis for moving forward and back.
    private string m_TurnAxisName;              // The name of the input axis for turning.
    private Rigidbody m_Rigidbody;              // Reference used to move the tank.
    private float m_MovementInputValue;         // The current value of the movement input.
    private float m_TurnInputValue;             // The current value of the turn input.
    private float m_OriginalPitch;              // The pitch of the audio source at the start of the scene.
    private ParticleSystem[] m_particleSystems; // References to all the particles systems used by the Tanks

    [FMODUnity.EventRef]
    public string MovEvent;
    FMOD.Studio.EventInstance Mov;
    public string IdleEvent;
    FMOD.Studio.EventInstance Idle;

    private void Awake ()
    {
        m_Rigidbody = GetComponent<Rigidbody> ();
    }


    private void OnEnable ()
    {
        // When the tank is turned on, make sure it's not kinematic.
        m_Rigidbody.isKinematic = false;

        // Also reset the input values.
        m_MovementInputValue = 0f;
        m_TurnInputValue = 0f;

        // We grab all the Particle systems child of that Tank to be able to Stop/Play them on Deactivate/Activate
        // It is needed because we move the Tank when spawning it, and if the Particle System is playing while we do that
        // it "think" it move from (0,0,0) to the spawn point, creating a huge trail of smoke
        m_particleSystems = GetComponentsInChildren<ParticleSystem>();
        for (int i = 0; i < m_particleSystems.Length; ++i)
        {
            m_particleSystems[i].Play();
        }
    }


    private void OnDisable ()
    {
        // When the tank is turned off, set it to kinematic so it stops moving.
        m_Rigidbody.isKinematic = true;

        // Stop all particle system so it "reset" it's position to the actual one instead of thinking we moved when spawning
        for(int i = 0; i < m_particleSystems.Length; ++i)
        {
            m_particleSystems[i].Stop();
        }
    }


    private void Start ()
    {
        // The axes names are based on player number.
        m_MovementAxisName = "Vertical" + m_PlayerNumber;
        m_TurnAxisName = "Horizontal" + m_PlayerNumber;

        // Store the original pitch of the audio source.
        m_OriginalPitch = m_MovementAudio.pitch;
    }


    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)
        {
            // ... and if the audio source is currently playing the driving clip...
            if (m_MovementAudio.clip == m_EngineDriving)
            {
                // ... change the clip to idling and play it.
                m_MovementAudio.clip = m_EngineIdling;
                m_MovementAudio.pitch = Random.Range (m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
                m_MovementAudio.Play ();

                // FMOD Event
                Mov.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
                Idle = FMODUnity.RuntimeManager.CreateInstance(IdleEvent);
                Idle.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
                Idle.start();
                print("IdleStart");
			}	
        }
        else
        {
            // Otherwise if the tank is moving and if the idling clip is currently playing...
            if (m_MovementAudio.clip == m_EngineIdling)
            {
                // ... change the clip to driving and play.
                m_MovementAudio.clip = m_EngineDriving;
                m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
                m_MovementAudio.Play();

                // FMOD Event   
                Idle.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
                Mov = FMODUnity.RuntimeManager.CreateInstance(MovEvent);
                Mov.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
                Mov.start();
                print("MovStart");
            }
        }
    }
	
	

    private void FixedUpdate ()
    {
        // Adjust the rigidbodies position and orientation in FixedUpdate.
        Move ();
        Turn ();
    }


    private void Move ()
    {
        // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
        Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

        // Apply this movement to the rigidbody's position.
        m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
    }


    private void Turn ()
    {
        // Determine the number of degrees to be turned based on the input, speed and time between frames.
        float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;

        // Make this into a rotation in the y axis.
        Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);

        // Apply this rotation to the rigidbody's rotation.
        m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
    }
}

}

From what I understand of your script - one of the AudioClips you’re using will always be playing. The code you have for starting and stopping FMOD events appears fine but I’m not sure that is the issue here.

Could you please check the following:

  • Does your driving condition ever fully reach the conditions for being considered “idle” after being in a “driving” state? Are you able to print the values of Mathf.Abs (m_MovementInputValue) and Mathf.Abs (m_TurnInputValue)?
  • Could you record an FMOD Studio Live Update session and see if these events are actually stopping & starting as expected?
  • Attach a Visual Studio session to your Unity project and put breakpoints on the FMOD events being stopped. Ensure you are reaching these and no errors are being thrown.