EasyCastleUNITY

Hero Shooter 새로운 Input System 활용한 조이스틱 이동 본문

유니티 심화

Hero Shooter 새로운 Input System 활용한 조이스틱 이동

EasyCastleT 2023. 9. 1. 10:42

만든 Actions
인스펙터

On-Screen Stick 컴포넌트에서 경로를 지정해야 한다.

Player Input 컴포넌트의 Behavior는 Invoke C Sharp Event를 사용한다. 

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

public class TestInputManager : MonoBehaviour
{
    [SerializeField] Transform playerTransform;

    private Vector3 moveDir;
    private Animator anim;

    private PlayerInput playerInput;
    private InputActionMap mainActionMap;
    private InputAction moveAction;
    

    private void Start()
    {
        this.anim = this.playerTransform.gameObject.GetComponent<Animator>();

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

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

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

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

    private void Update()
    {
        if(this.moveDir != Vector3.zero)
        {
            this.playerTransform.rotation = Quaternion.LookRotation(this.moveDir);
            this.playerTransform.Translate(Vector3.forward * Time.deltaTime * 4.0f);
        }
    }
    #region SEND_MESSAGE
    public void OnMove(InputValue value)
    {
        Vector2 dir = value.Get<Vector2>();
        this.moveDir = new Vector3(dir.x, 0, dir.y);
        Debug.Log("SEND_MESSAGE 실행");
        Debug.Log(dir);

        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);
        Debug.Log("UNITY_EVENTS 실행");
        Debug.Log(dir);

        this.anim.SetInteger("State", 1);
    }
    #endregion
}

실행영상

'유니티 심화' 카테고리의 다른 글

Hero SHooter 개발일지 2  (0) 2023.09.03
유니티 데이터 연동  (0) 2023.09.01
Hero Shooter new Input System Test  (0) 2023.08.31
HeroShooter 개발일지1  (0) 2023.08.30
HeroShooter 중간과정 정리  (0) 2023.08.29