24.02.26 TIL - Unity : Input Action, 2D 환경 이동, 상호작용 코드

JJwoo·2024년 2월 26일

Invoke Unity Event 사용


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

public class Player : MonoBehaviour
{
    private Vector2 moveDirection;
    [SerializeField]
    private float moveSpeed = 4f;

    void Update()
    {
        if (moveDirection != Vector2.zero)
        {
            
            transform.Translate(new Vector3(moveDirection.x, moveDirection.y, 0) * moveSpeed * Time.deltaTime);
        }
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        moveDirection = context.ReadValue<Vector2>();
        if (moveDirection != Vector2.zero)
        {
            Debug.Log($"UNITY_EVENTS : {moveDirection.magnitude}");
        }
    }

    public void OnInteraction(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            // 상호작용 로직
            Debug.Log("상호작용");
        }
    }
}

접근 제한자 public에 유의
profile
개발 모코코

0개의 댓글