'회전'에 해당되는 글 1건

[Unity] 가로 Swipe 판단

Unity 2023. 1. 22. 17:44

목표: 가로 Swipe를 토대로 캐릭터 카메라 회전

  • Event에 익숙해지기 위해서 Event 사용
    • Scene이 바뀌어도 딱히 문제 없이 사용하기 위함
private Coroutine iCoroutine_Input = null;
public void StartInput()
{
    this.iCoroutine_Input = this.StartCoroutine(this.IENInput());
}
public event System.Action<float> eSwipeHorizontal = null;
private IEnumerator IENInput()
{
    while (true)
    {
        // :: Swipe Horizontal
        // :: 처음 터치
        if (Input.GetMouseButtonDown(0))
        {
            // :: 터치 시작 위치
            Vector2 startPos = Input.mousePosition;
            // :: 터치 중
            while (Input.GetMouseButton(0))
            {
                // :: 터치 끝 위치
                Vector2 endPos = Input.mousePosition;
                // :: 터치 방향
                Vector2 swipe = endPos - startPos;
                // :: 터치 방향 이벤트
                this.eSwipeHorizontal?.Invoke(swipe.x);
                yield return null;
            }
        }
        yield return null;
    }
}
public void StopInput()
{
    if (this.iCoroutine_Input != null)
    {
        this.StopCoroutine(this.iCoroutine_Input);
        this.iCoroutine_Input = null;
    }
}

 

블로그 이미지

RIsN

,