반응형

유니티 Input System Package 사용방법.

 

Workflow - Actions

대부분의 경우 권장되는 방법이다.

Responding to Actions

각 입력에 대해 반응하는 방법이다.

Method Description
InputAction.WasPerformedThisFrame() True if the InputAction.phase of the action has, at any point during the current frame, changed to Performed.
InputAction.WasCompletedThisFrame() True if the InputAction.phase of the action has, at any point during the current frame, changed away from Performed to any other phase. This can be useful for Button actions or Value actions with interactions like Press or Hold when you want to know the frame the interaction stops being performed. For actions with the default Interaction, this method will always return false for Value and Pass-Through actions (since the phase stays in Started for Value actions and stays in Performed for Pass-Through).
InputAction.IsPressed() True if the level of actuation on the action has crossed the press point and did not yet fall to or below the release threshold.
InputAction.WasPressedThisFrame() True if the level of actuation on the action has, at any point during the current frame, reached or gone above the press point.
InputAction.WasReleasedThisFrame() True if the level of actuation on the action has, at any point during the current frame, gone from being at or above the press point to at or below the release threshold.

 

void Update()
{
    if (isDead)
        return;

    if (jumpAction.WasPerformedThisFrame() && jumpCount < 2)
    {
        // 버튼이 눌릴때 한 번
        jumpCount++;
        playerRigidbody.linearVelocity = Vector2.zero;
        playerRigidbody.AddForce(new Vector2(0, jumpForce));
        playerAudio.Play();
    }
    else if (jumpAction.WasCompletedThisFrame() && playerRigidbody.linearVelocity.y > 0)
    {
        // 버튼이 떨어질때 한 번
        playerRigidbody.linearVelocity = playerRigidbody.linearVelocity * 0.5f;
    }

    animator.SetBool("Grounded", isGrounded);
}

예제 코드

 

Workflow - Actions and PlayerInput

Callback 함수를 사용하는 방법이다.

Callback 함수를 연결하는 방법은 아래를 참고한다.

 

Player Input 컴포넌트에 Input Action Asset을 연결한다.

 

Project-wide Input Action을 Player Input에 사용 하는건 권장되지 않는 방법이지만 여기서는 사용 방법을 보이기 위해 그냥 사용했다. Behavior는 Invoke Unity Events로 변경하고, Events - Player - Move(CallbackContext)에 'Player' 오브젝트가 연결되어 있는데 # 아이콘이 마치 스크립트인것 같지만 실제로는 Callback 함수가 정의된 스크립트를 가지는 Player 게임 오브젝트이다.

 

이 Player 게임 오브젝트에 PlayerController라는 스크립트가 추가되어 있고 그 스크립트에 OnMove()가 존재한다. 오른쪽에 PlayerController.OnMove를 선택해 주었다. (이 예에선 Player 게임 오브젝트가 Player Input Asset도 가지고 있고 PlayerController 스크립트도 가지고 있는 것이다)

 

PlayerController 스크립트에 OnMove()가 정의 되어 있다.

 

반응형
Posted by J-sean
: