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;
}
}
}