[Unity] UniRx - 키보드 입력

Will-Big·2023년 1월 9일
0

유니티

목록 보기
1/4

UniRx 를 사용한 2D 캐릭터 이동

    void Start()
    {
        this.UpdateAsObservable() // Horizontal Move
            .Select(_ => new Vector2(Input.GetAxis("Horizontal"), 0))
            .Subscribe(x => Move(x));

        this.UpdateAsObservable() // Jump
            .Where(_ => curJumpCount > 0 && Input.GetKeyDown(KeyCode.W))
            .Subscribe(x => Jump());
        
        this.UpdateAsObservable() // Left Dash
            .Where(_ => !isDashed && Input.GetKeyDown(KeyCode.A))
            .Buffer(System.TimeSpan.FromMilliseconds(250))
            .Where(_ => _.Count >= 2)
            .Subscribe(_ => Dash(-1);
        
        this.UpdateAsObservable() // Right Dash
            .Where(_ => !isDashed && Input.GetKeyDown(KeyCode.D))
            .Buffer(System.TimeSpan.FromMilliseconds(250))
            .Where(_ => _.Count >= 2)
            .Subscribe(_ => Dash(1);
    }

좋았던 점

  1. 구현이 쉬움
  2. Update 문을 돌지 않기 때문에 성능상 이점이 있음
  3. 멤버 변수를 적게 사용하여 함수 간 의존성이 낮음

아쉬운 점

  1. 대쉬에서 0.25초가 반드시 지나야 수행하는 듯 함
    1-1. 위 문제로 입력 감지가 느린 느낌이 있음
  2. UniRx 활용법이 미숙함
profile
개발자가 되고싶어요

0개의 댓글