How to loop AudioClip after finished audio

What im looking for is to loop an audioclip as I hold down a button. I’m instantiating an audioclip from another script with input.getbuttondown but once the audio finishes it deletes, I need it to loop unit i let off the button. Any advice will be thankful

void Update () 
	{
		//transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * speed * Time.deltaTime);
		//transform.Translate (Vector3.right * Input.GetAxis ("Horizontal") * speed * Time.deltaTime);

		if(Input.GetButtonDown("Vertical") || Input.GetButtonDown("Horizontal"))
		{

			SoundEffect.Instance.MakeWalkSound ();

		}
	}

***** SoundEffect script from where i instantiate an AudioClip from.*****

using UnityEngine;
using System.Collections;

public class SoundEffect : MonoBehaviour 
{

	public static SoundEffect Instance;

	public AudioClip WalkSound;
	public AudioClip WinningSound;
	public AudioClip JumpSound;
	public AudioClip DoorOpenSound;
	public AudioClip WarningSound;


	void Awake()
	{
		Instance = this;
	}

	public void MakeSound(AudioClip originalCLip)
	{
		AudioSource.PlayClipAtPoint (originalCLip, transform.position);

	}


	public void MakeWalkSound()
	{
		MakeSound (WalkSound);

	}

	public void MakeJumpSound()
	{
		MakeSound (JumpSound);
	}

	public void MakeDoorOpenSound()
	{
		MakeSound (DoorOpenSound);
	}


}

1 Answer

1

Wouldn’t this create a ‘SoundEffect’ object every frame you have a button down? Probably not what you are looking for I would guess.

I would attach and audio source to your character with a looping clip attached to it, when the player presses the ‘walk’ button call Play() on the audioSource, when they release call Stop()

You might use multiple audioSources with different clips put in each, but it could work with a single source as well…