유니티 C# 모바일 화면 더블 클릭 터치 간단 구현 Double Click

반응형

코드 작성

using UnityEngine;

public class DoubleTapInteraction : MonoBehaviour
{
    private float lastTapTime = 0f;
    private float doubleTapTime = 0.5f; // 더블 탭을 인식하는 시간 간격

    void Update()
    {
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Ended)
            {
                float currentTime = Time.time;
                if (currentTime - lastTapTime < doubleTapTime)
                {
                    // 더블 탭으로 인식
                    OnDoubleTap();
                }
                lastTapTime = currentTime;
            }
        }
    }

    void OnDoubleTap()
    {
        // 더블 탭 시 수행할 동작을 여기에 구현
        Debug.Log("Double tap detected!");
        // 예: 특정 오브젝트와 상호작용
        // InteractionWithObject();
    }

    // 추가: 상호작용을 위한 함수
    void InteractionWithObject()
    {
        // 예: 특정 오브젝트를 찾아서 상호작용
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

        if (Physics.Raycast(ray, out hit))
        {
            // 상호작용할 오브젝트를 찾았을 때 처리
            Debug.Log("Interacted with: " + hit.collider.name);
            // hit.collider.GetComponent<YourComponent>().YourMethod();
        }
    }
}
반응형