Unity C# Problem

In my code, I’m trying to make a sound play once my train hits a certain speed. For example, once it hits 0.06f, play a fast moving sound. However, it just sounds broken. I believe its because its being called to fast.

How could I make it play only once, or just play once it hits 0.06f?

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour {
public float mSpeed;
public float rSpeed;
public AudioSource move;

void Start () {
	mSpeed = 0f;
	rSpeed = .0001f;
}

// Update is called once per frame
void Update () {

            transform.Translate (-mSpeed, 0, 0);

          // Problem Area !!
	if (mSpeed <= 0.06f) 
	{
		move.Play ();
	}
          // Problem Area !!

	if (Input.GetKey ("w")) 
	{
		mSpeed += rSpeed;
	}

	if (Input.GetKey ("s")) 
	{
		mSpeed += -rSpeed;
	}
}

}

You want to use a property for that, it’s the best way to trigger things only when a value changes.

private float _mSpeed;
public float mSpeed
{
    get { return _mSpeed; }
    set
    {
        if (value != _mSpeed) // if value changed
        {
            if (_mSpeed <= 0.06f && value > 0.06f) // if previous value is less than and new value is greater than 0.06
            {
                move.Play ();
            }
            _mSpeed = value;
        }
    }
}

void Update ()
{
    transform.Translate (-mSpeed, 0, 0);
    if (Input.GetKey ("w")) 
    {
        mSpeed += rSpeed;
    }
    if (Input.GetKey ("s")) 
    {
        mSpeed += -rSpeed;
    }
 }