You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1014 B
C#

using UnityEngine;
using UnityEngine.InputSystem;
namespace Class03
{
// 키보드를 읽어 이동 방향만 전달합니다.
public sealed class KeyboardMoveInput : MonoBehaviour
{
public Vector2 Direction { get; private set; }
private void Update()
{
Keyboard keyboard = Keyboard.current;
if (keyboard == null || !Application.isFocused)
{
Direction = Vector2.zero;
return;
}
float x = (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed ? 1 : 0)
- (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed ? 1 : 0);
float y = (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed ? 1 : 0)
- (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed ? 1 : 0);
Direction = Vector2.ClampMagnitude(new Vector2(x, y), 1f);
}
private void OnDisable() => Direction = Vector2.zero;
}
}