EasyCastleUNITY

Hero Shooter new Input System Test 본문

유니티 심화

Hero Shooter new Input System Test

EasyCastleT 2023. 8. 31. 18:14

만든 Input Action
player input 컴포넌트 응용

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Windows;

public class TestBindPlayerController : MonoBehaviour
{
    private Vector3 moveDir;
    private Animator anim;

    private PlayerInput playerInput;
    private InputActionMap mainActionMap;
    private InputAction moveAction;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();

        this.playerInput = GetComponent<PlayerInput>();
        this.mainActionMap = this.playerInput.actions.FindActionMap("PlayerActions");

        this.moveAction = this.mainActionMap.FindAction("Move");

        this.moveAction.performed += (context) =>
        {
            Vector2 dir = context.ReadValue<Vector2>();
            this.moveDir = new Vector3(dir.x, 0, dir.y);
            this.anim.SetInteger("State", 1);
        };
        this.moveAction.canceled += (context) =>
        {
            this.moveDir = Vector3.zero;
            this.anim.SetInteger("State", 0);
        };
    }

    // Update is called once per frame
    void Update()
    {
        if(this.moveDir != Vector3.zero)
        {
            this.transform.rotation = Quaternion.LookRotation(this.moveDir);
            this.transform.Translate(Vector3.forward * Time.deltaTime * 4.0f);
        }
    }
    #region SEND_MESSAGE
    void OnMove(InputValue value)
    {
        Vector2 dir = value.Get<Vector2>();
        Debug.LogFormat("dir:{0}", dir);
        this.moveDir = new Vector3(dir.x, 0, dir.y);

        this.anim.SetInteger("State", 1);
    }
    #endregion
    #region UNITY_EVENTS
    public void OnMove(InputAction.CallbackContext context)
    {
        Vector2 dir = context.ReadValue<Vector2>();
        this.moveDir = new Vector3(dir.x, 0, dir.y);
        this.anim.SetInteger("State", 1);
    }
    #endregion
}

Direct Binding

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

public class TestDirectBindPlayerController : MonoBehaviour
{
    private InputAction moveAction;

    private Animator anim;
    private Vector3 moveDir;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        //1.액션 만들기 
        this.moveAction = new InputAction("Move",InputActionType.Value);

        //2.바인딩 
        this.moveAction.AddCompositeBinding("2DVector")
        .With("Up", "<Keyboard>/w")
        .With("Down", "<Keyboard>/s")
        .With("Left", "<Keyboard>/a")
        .With("Right", "<Keyboard>/d");

        //3.이벤트 붙이기 
        this.moveAction.performed += (context) => {
            Vector2 dir = context.ReadValue<Vector2>();
            this.moveDir = new Vector3(dir.x, 0, dir.y);
            this.anim.SetInteger("State", 1);
        };

        this.moveAction.canceled += (context) => {
            this.moveDir = Vector3.zero;
            this.anim.SetInteger("State", 0);
        };

        //4. 활성화
        this.moveAction.Enable();
    }

    // Update is called once per frame
    void Update()
    {
        if (this.moveDir != Vector3.zero)
        {
            //회전
            transform.rotation = Quaternion.LookRotation(this.moveDir);
            //회전한 후 전진 방향으로 이동
            transform.Translate(Vector3.forward * Time.deltaTime * 4.0f);
        }
    }
}

키보드 움직임

조이스틱으로 움직이는 것은 좀더 찾아보고 테스트 해봐야 겠다.