유니티 리듬게임 제작 (메트로놈 제작) 관련 질문입니다.

조회수 325회

노트 구현을 하기 전에 메트로놈 제작이 먼저 필요하다고 해서 테스트할 오브젝트 하나 생성하고 박자가 잘 맞게 되고 있는지 확인하려고 콘솔에 흐른 시간을 출력하도록 코드를 작성했는데요.

출력된 시간이 정확하게 0.25가 나와야 하는데 약간씩 다 오차가 있어서 이를 해결할 방법을 모르겠습니다. 구글링 했을 때 timeSamples 값에 대한 내용이 있었는데 코드 내용이 일부만 나와있는거 같아서 이해가 잘 되지는 않네요.

구글링 한 거: https://healp.tistory.com/8

아래는 메트로놈 스크립트 코드입니다.

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

public class BeatTest : MonoBehaviour
{
    public AudioSource audioSource;
    public GameObject nemo;

    float musicbpm = 120f;
    float stdbpm = 60;
    float musictempo = 4f;
    float stdtempo = 8f;

    float tikTime = 0;
    float nextTime = 0;



    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        tikTime = (stdbpm / musicbpm) * (musictempo / stdtempo);

        nextTime += Time.deltaTime;

        if(nextTime >= tikTime){
            StartCoroutine(PlayTik(tikTime));
            nextTime-=tikTime;
        }
    }

    IEnumerator PlayTik(float tikTime)
    {
        audioSource.Play();
        Debug.Log(nextTime);
        transform.Rotate(Vector3.back * 45);
        yield return new WaitForSeconds(tikTime);
    }


}

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)