👾 유니티/⌨️ 키보드파이터 개발일지

⌨️ 키보드 파이터 개발일지 #9 사운드 넣기

Buᐢ༝ᐢy 2023. 3. 30. 18:00

<aside> ⚠️ 해당 프로젝트는 10일 안에 빠르게 끝낼 예정으로 기획했으므로 잘 짜여진 구조의 프로젝트가 아니며 검색을 통해 프로젝트를 진행하고 있다.

</aside>

2023년 3월 25일

public class Player : MonoBehaviour
{	
		[SerializeField]
    AudioClip stepSound;

    AudioSource audioSource;

		void Start()
    {
        audioSource = GetComponent<AudioSource>();
		}

		private void Move()
    {
				// 중략

        if (isMove)
        {
            if (!audioSource.isPlaying)
                audioSource.PlayOneShot(stepSound);

				// 중략

				}
		}
}

소리를 내는 오브젝트에 **AudioSource** 컴포넌트를 넣어줘야 한다. 안 그러면 정상 작동하지 않고 오류가 난다.

소리는 나지만 타이밍이 이상하다… 그래서 코드를 조금 손보았다.

public class Attackable : MonoBehaviour
{
    [SerializeField]
    GameObject keyboard;
    [SerializeField]
    TextMeshProUGUI[] score;
    [SerializeField]
    AudioClip attackSound;

    AudioSource audioSource;

    void Start()
    {
        audioSource = keyboard.transform.gameObject.GetComponent<AudioSource>();
        audioSource.clip = attackSound;
		}

		// 중략

		IEnumerator Attack()
    {
        //if (!audioSource.isPlaying)
          audioSource.Play();
            
          yield return new WaitForSeconds(attackTime);
          keyboardCollider.enabled = false;
          isDelay = false;
    }
}

공격하는 코루틴 사이에 넣어주었다. 사운드가 끝나는 부분에 여백 시간이 1초 안 되는데 살짝 거슬려서 **isPlaying** 을 조건문으로 걸지 않고 바로 **play** 시켰다. 한 번만 해주는 **playOneShot** 역시 사용하지 않았다. 게다가 오디오 소스는 게임오브젝트당 하나만 가능해서 발소리와 같이 나게 되는 조건이라면 둘 중 먼저 발동되는 소리만 나오기 때문에 키보드에 넣어주었다.

</aside>

2023년 3월 25일

public class Player : MonoBehaviour
{	
		[SerializeField]
    AudioClip stepSound;

    AudioSource audioSource;

		void Start()
    {
        audioSource = GetComponent<AudioSource>();
		}

		private void Move()
    {
				// 중략

        if (isMove)
        {
            if (!audioSource.isPlaying)
                audioSource.PlayOneShot(stepSound);

				// 중략

				}
		}
}

소리를 내는 오브젝트에 **AudioSource** 컴포넌트를 넣어줘야 한다. 안 그러면 정상 작동하지 않고 오류가 난다.

소리는 나지만 타이밍이 이상하다… 그래서 코드를 조금 손보았다.

public class Attackable : MonoBehaviour
{
    [SerializeField]
    GameObject keyboard;
    [SerializeField]
    TextMeshProUGUI[] score;
    [SerializeField]
    AudioClip attackSound;

    AudioSource audioSource;

    void Start()
    {
        audioSource = keyboard.transform.gameObject.GetComponent<AudioSource>();
        audioSource.clip = attackSound;
		}

		// 중략

		IEnumerator Attack()
    {
        //if (!audioSource.isPlaying)
          audioSource.Play();
            
          yield return new WaitForSeconds(attackTime);
          keyboardCollider.enabled = false;
          isDelay = false;
    }
}

공격하는 코루틴 사이에 넣어주었다. 사운드가 끝나는 부분에 여백 시간이 1초 안 되는데 살짝 거슬려서 **isPlaying** 을 조건문으로 걸지 않고 바로 **play** 시켰다. 한 번만 해주는 **playOneShot** 역시 사용하지 않았다. 게다가 오디오 소스는 게임오브젝트당 하나만 가능해서 발소리와 같이 나게 되는 조건이라면 둘 중 먼저 발동되는 소리만 나오기 때문에 키보드에 넣어주었다.