Rolling Sound in c#

Hi,

i work with the roll a ball game prefab and try to build a rolling sound in c#
so far i got this:

		if (rigidbody.velocity.magnitude > 0.5  /* Physics.Raycast(transform.position, Vector3.down, height) gameObject.tag == "Untagged"*/)
		{

			_GameManager.GetComponent<GameManager>().BallRollSlow();
		}
	
			
		if (rigidbody.velocity.magnitude > 4.5 /* Physics.Raycast(transform.position, Vector3.down, height) gameObject.tag == "Untagged"*/)
		{
			_GameManager.GetComponent<GameManager>().BallRollFast(); 
		}

but this make a horrible fast sound attack when i rolling the ball.

I know i need a if(audio.isPlaying) somewhere to check “Is the clip currently playing”
but i have no luck with my experiments.

any ideas, please?

Welcome to the forums! Congrats on your first post,

You’re not showing any code that tells us how you’re playing your audio. Looped sounds (like a rolling ball) can be tricky to get right. Typically, managing sound effects for any state-machine-like object should be accomplished through the use of a configurable set of static methods. Maybe even a singleton AudioManager once you need that much control. Over the course of your Unity career, by necessity you’ll create some highly re-usable methods for toggling between two versions of a similar sound, as seen here, and many other common tasks like cross-fading, pitch shifting, volume fading, and so on.

Now in your case, all that’s probably overkill. That said, you’ll have to more carefully describe the issue you’re encountering, share the appropriate code, and if you can, share a webplayer demo or video for us to analyze.

Cheers,

Ah, i see:
This is the GameManager:

using UnityEngine;
using System.Collections;

public class GameManager: MonoBehaviour 
{
	private AudioSource sound;
	private GameObject Door;
	private bool won;
	private bool death;
	public GUIStyle guiStyle;
	public string NextLevel;
	
	public int totalCoin;
	public int foundCoin;
	
	public AudioClip DestroySound;
	public AudioClip WonSound;
	public AudioClip CoinSound;
	public AudioClip SpeedBoosterSound;
	public AudioClip JumpBoosterSound;
	public AudioClip TeleporterSound;
	public AudioClip BallJumpSound;
	public AudioClip BallHitGroundSound;
	public AudioClip BallRollSlowSound;
	public AudioClip BallRollFastSound;
	
	void Start () 
	{
		Time.timeScale = 1.0f;
		totalCoin = GameObject.FindGameObjectsWithTag("Coin").Length;
		Door = GameObject.Find("Door");
		sound = GetComponent<AudioSource>();
	}
	
	void Updata()
	{
		if (Input.GetKey (KeyCode.X))
		{
			Application.LoadLevel("Main_Menu");
		}
	}
	
	void OnGUI () 
	{
		if (GUI.Button(new Rect(10, Screen.height - 25, 50, 20), "Menu"))
		{            
			Application.LoadLevel("Main_Menu");
		}
		
		if (won) 
		{
			GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 150, 100, 20), "You Won", guiStyle);
			if(NextLevel != "Main_Menu")
			{
				if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 130, 100, 20), "Next Level"))
				{            
					Application.LoadLevel(NextLevel);
				}
			}
			if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 105, 100, 20), "Menu"))
			{            
				Time.timeScale = 1f;
				Application.LoadLevel("Main_Menu");
			}
		}
		
		else if (death) 
		{
			GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 150, 100, 20), "You're Dead", guiStyle);
			if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 130, 100, 20), "Try Again"))
			{            
				Application.LoadLevel(Application.loadedLevelName);
			}
			if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 105, 100, 20), "Menu"))
			{            
				Application.LoadLevel("Main_Menu");
			}
		}
		GUI.Label(new Rect(10, 10, 500, 20), "Found Coins: "+foundCoin+"/"+totalCoin, guiStyle);
	}
	
	public void FoundCoin()
    {
		audio.clip = CoinSound;
		audio.Play();
        foundCoin++;
		
        if (foundCoin >= totalCoin)
        {
            Door.GetComponent<Door>().FindAllCoin = true;
        }
    }
	
		public void SpeedBooster()
    {
		audio.clip = SpeedBoosterSound;
		audio.Play();
	}
	
	public void BallRollSlow()
    {
		audio.clip = BallRollSlowSound;
		audio.loop = true;
	//	audio.PlayOneShot();
		audio.Play();
	}
	
		public void BallRollFast()
    {
		audio.clip = BallRollFastSound;
	//	audio.PlayOneShot();
		audio.loop = true;
		audio.Play();
	}
	
	
	public void JumpBooster()
    {
		audio.clip = JumpBoosterSound;
		audio.Play();
	}
	
	public void BallJump()
    {
		audio.clip = BallJumpSound;
		audio.Play();
	}
	
	public void BallHitGround()
    {
		audio.clip = BallHitGroundSound;
		audio.Play();
	}
	
	public void Teleporter()
    {
		audio.clip = TeleporterSound;
		audio.Play();
	}
	
	public void Won()
	{
		audio.clip = WonSound;
		audio.Play();
		Time.timeScale = 0f;
		won = true;
	}
	
	public void Death()
	{
		audio.clip = DestroySound;
		audio.Play();
		death = true;
	}
	

}

This is the PlayerScript:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
	private GameObject moveJoy;
	private GameObject _GameManager;
	public Vector3 movement;
	public float moveSpeed = 6.0f;
	public float jumpSpeed = 5.0f;
	public float drag = 3;
	private bool canJump = true;
	//public AudioClip BallRollSlowSound;
	//public float height;


	
	
	
	void Start()
	{
		moveJoy = GameObject.Find("LeftJoystick");
		_GameManager = GameObject.Find("_GameManager");
		// height = collider.bounds.extents.y + 0.2f;  //Landi
	}
	
	void Update () 
	{	
		Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
		forward.y = 0;
		forward = forward.normalized;

		
		Vector3 forwardForce = new Vector3();
		if (Application.platform == RuntimePlatform.Android) 
		{
			float tmpSpeed = moveJoy.GetComponent<Joystick>().position.y;
			forwardForce = forward * tmpSpeed * 1f * moveSpeed;
		}
		else
		{
			forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;

			
		}
		rigidbody.AddForce(forwardForce);
		
		
		Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
		right.y = 0;
		right = right.normalized;
		
		Vector3 rightForce = new Vector3();
		if (Application.platform == RuntimePlatform.Android) 
		{
			float tmpSpeed = moveJoy.GetComponent<Joystick>().position.x;
			rightForce = right * tmpSpeed * 0.8f * moveSpeed;
		}
		else
		{
			rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
		}		
		rigidbody.AddForce(rightForce);

		
		if (canJump  Input.GetKeyDown(KeyCode.Space))
		{
			rigidbody.AddForce(Vector3.up * jumpSpeed * 100);
			canJump = false;
			_GameManager.GetComponent<GameManager>().BallJump();
		}
		
		
	
	
		if (rigidbody.velocity.magnitude > 0.5  /* Physics.Raycast(transform.position, Vector3.down, height) gameObject.tag == "Untagged"*/)
		{

			_GameManager.GetComponent<GameManager>().BallRollSlow();
		}
	
			
		if (rigidbody.velocity.magnitude > 4.5 /* Physics.Raycast(transform.position, Vector3.down, height) gameObject.tag == "Untagged"*/)
		{
			_GameManager.GetComponent<GameManager>().BallRollFast(); 
		}

	
		
	}
	
	void OnTriggerEnter(Collider other) 
	{
		if (other.tag == "Destroy")
		{
			_GameManager.GetComponent<GameManager>().Death();
			Destroy(gameObject);
		}
		else if (other.tag == "Coin")
		{
			Destroy(other.gameObject);
			_GameManager.GetComponent<GameManager>().FoundCoin();
		}
		else if (other.tag == "SpeedBooster")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().SpeedBooster();
		}
		else if (other.tag == "JumpBooster")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().JumpBooster();
		}
		else if (other.tag == "Teleporter")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().Teleporter();
		}
    }
	
	void OnCollisionEnter(Collision collision)
	{
		if (!canJump)
		{
			canJump = true;
			_GameManager.GetComponent<GameManager>().BallHitGround();
		}
    }
    

	
	void OnGUI()
	{
		GUI.Label(new Rect(300,10,100,100),"X: " + moveJoy.GetComponent<Joystick>().position.x.ToString());
		GUI.Label(new Rect(300,30,100,100),"Y: " + moveJoy.GetComponent<Joystick>().position.y.ToString());
	}
}

My Idea was, that i do not post to much useless info, sry.
But now, it should be good and enough info.

The code will help clarify, but I still don’t have an understanding of the issue. Try to describe it using appropriate jargon wherever possible. Please describe what is meant by “a horrible fast sound attack” so others may understand what’s happening.

See(hear) the video

It sounds like the sound always started again and again without ending before, but it should looping after ending the soundfile for a nice rolling sound.
you hear the real sound short before the ball is stopping.

thanks for helping me.

Gottcha. Yeah, that’s probably what’s happening. But it seems like you’ve solved your own problem, no? The most simple fix is to check to see if it’s already playing, then play it only if it’s not. Since you’re performing this check every Update(), there will probably be no interruption when the sound stops and is started again as long as the audio source is properly looped. Anything more will require you to code new features. Stopping the sound immediately if the ball isn’t grounded, for instance. Once you’ve got all that working, you can try interpolating the volume and pitch based on velocity for added believability!

I am currently here:

	public AudioClip BallRollSlowSound;
	public AudioClip BallRollFastSound;
		if ( !audio.isPlaying    audio.clip != BallRollSlowSound  rigidbody.velocity.magnitude > 0.5   Physics.Raycast(transform.position, Vector3.down, height)/* gameObject.tag == "Untagged"*/)
		{
	//		_GameManager.GetComponent<GameManager>().BallRollFast();	
				audio.clip = BallRollSlowSound;
				audio.loop = true;
				audio.Play();
				audio.volume = rigidbody.velocity.magnitude * 0.2f;
		}
	
			
		if ( !audio.isPlaying    audio.clip != BallRollFastSound  rigidbody.velocity.magnitude > 4.5  Physics.Raycast(transform.position, Vector3.down, height)/* gameObject.tag == "Untagged"*/)
		{
			
			    audio.clip = BallRollFastSound;
				audio.loop = true;
				audio.Play();
				audio.volume = rigidbody.velocity.magnitude * 0.2f;
	//		_GameManager.GetComponent<GameManager>().BallRollFast(); 
		}	
	
	}

No Sound anymore :frowning:

My Solution so far:

		void OnCollisionStay( Collision collision_info )
	{
		 if( collision_info.gameObject.GetComponent<MeshCollider>() != null )
		{
			if ( ( !audio.isPlaying )  ( audio.clip != BallRollSlowSound ) )
			{
			_GameManager.GetComponent<GameManager>().BallRollSlow();
			}
			audio.volume = rigidbody.velocity.magnitude * 0.2f;
		}

        mIsGrounded = true;
	}

Is there a better solution?

Next i work on the check if the ball in the air. :slight_smile: