Component - Player Input
#region
:코드 접기
SendMessage
Invoke Unity Events
Invoke C Sharp Events
WarriorController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WarriorController : MonoBehaviour
{
private Animator anim;
private Vector3 moveDir;
private Transform transform;
private PlayerInput playerInput;
private InputActionMap mainActionMap;
private InputAction moveAction;
private InputAction attackAction;
private void Start()
{
this.anim = this.GetComponent<Animator>();
this.transform = this.GetComponent<Transform>();
this.playerInput = this.GetComponent<PlayerInput>();
//ActionMap 추출
this.mainActionMap = this.playerInput.actions.FindActionMap("PlayerActions");
//Move, Attack 액션 추출
this.moveAction = this.mainActionMap.FindAction("Move");
this.attackAction = this.mainActionMap.FindAction("Attack");
this.moveAction.performed += (context) => {
//이동
Vector2 dir = context.ReadValue<Vector2>();
this.moveDir = new Vector3(dir.x, 0, dir.y);
this.anim.SetFloat("Movement", dir.magnitude);
};
this.moveAction.canceled += (context) => {
//멈추기
this.moveDir = Vector3.zero;
this.anim.SetFloat("Movement", 0);
};
//공격
this.attackAction.performed += (context) => {
this.anim.SetTrigger("Attack");
};
}
private void Update()
{
if (this.moveDir != Vector3.zero)
{
this.transform.rotation = Quaternion.LookRotation(moveDir);
this.transform.Translate(Vector3.forward * 1f * Time.deltaTime);
}
}
#region SEND_MESSAGE
//SendMessage일 경우
//void On액션명
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.SetFloat("Movement", dir.magnitude);
}
void OnAttack()
{
Debug.Log("Attack");
this.anim.SetTrigger("Attack");
}
#endregion
#region UNITY_EVENTS
//method overloading
public void OnMove(InputAction.CallbackContext context)
{
Debug.LogFormat("context.phase: {0}", context.phase);
Vector2 dir = context.ReadValue<Vector2>();
this.moveDir = new Vector3(dir.x, 0, dir.y);
this.anim.SetFloat("Movement", dir.magnitude);
}
public void OnAttack(InputAction.CallbackContext context)
{
Debug.LogFormat("context.phase: {0}", context.phase);
if(context.performed)
{
Debug.Log("Attack");
this.anim.SetTrigger("Attack");
}
}
#endregion
}
WarriorDirectController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WarriorDirectController : MonoBehaviour
{
private InputAction moveAction;
private InputAction attackAction;
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);
this.attackAction = new InputAction("Attack", InputActionType.Button);
//2. 바인딩
//move action binding
InputActionSetupExtensions.CompositeSyntax syntax = this.moveAction.AddCompositeBinding("2DVector");
syntax = syntax.With("Up", "<Keyboard>/w");
syntax = syntax.With("Down", "<Keyboard>/s");
syntax = syntax.With("Left", "<Keyboard>/a");
syntax = syntax.With("Right", "<Keyboard>/d");
//attack action binding
this.attackAction.AddBinding("<Keyboard>/space");
//3. 이벤트 붙이기
//이동
this.moveAction.performed += (context) => {
Vector2 dir = context.ReadValue<Vector2>();
this.moveDir = new Vector3(dir.x, 0, dir.y);
//애니메이션 실행
this.anim.SetFloat("Movement", dir.magnitude);
};
//멈춤
this.moveAction.canceled += (context) => {
this.moveDir = Vector3.zero;
//애니메이션 실행
this.anim.SetFloat("Movement", 0);
};
//공격
this.attackAction.performed += (context) => {
Debug.Log("Attack");
//애니메이션 실행
this.anim.SetTrigger("Attack");
};
//4. 활성화
this.moveAction.Enable();
this.attackAction.Enable();
}
// Update is called once per frame
void Update()
{
if(this.moveDir != Vector3.zero)
{
//회전
this.transform.rotation = Quaternion.LookRotation(this.moveDir.normalized);
//이동
this.transform.Translate(Vector3.forward * 3f * Time.deltaTime);
}
}
}
'유니티 심화' 카테고리의 다른 글
조이스틱 인식오류 (0) | 2023.09.01 |
---|---|
HeroShooter - WASD /input Action (0) | 2023.08.31 |
절대강좌 유니티 - 오클루전 컬링 (0) | 2023.08.31 |
HeroShooter - 오브젝트 풀링[총알], 비동기[씬 전환] (0) | 2023.08.30 |
절대강좌 유니티 - 라이트매핑 및 라이트 프로브, 씬 관리, 동기 비동기 (0) | 2023.08.30 |