728x90
반응형
이번에는 Shift 키를 홀드하면 달릴 수 있게 만들었습니다.
Input Action을 하나 더 만든 뒤, Sprint를 Hold로 만들었습니다.

PlayerInputSO2.cs
using System;
using UnityEngine;
using UnityEngine.InputSystem;
namespace TopDownView.SecondPlayer
{
[CreateAssetMenu(fileName = "PlayerInput", menuName = "SO/PlayerInput2", order = 1)]
public class PlayerInputSO2 : ScriptableObject, Controls2.IPlayerActions
{
public event Action<Vector2> OnMovementChange;
public event Action OnAttackPressed;
public event Action OnJumpPressed;
public event Action<bool> OnRunPressed;
private Controls2 _controls;
private void OnEnable()
{
if (_controls == null)
{
_controls = new Controls2();
_controls.Player.SetCallbacks(this);
}
_controls.Player.Enable();
}
private void OnDisable()
{
_controls.Player.Disable();
}
public void OnMove(InputAction.CallbackContext context)
{
Vector2 input = context.ReadValue<Vector2>();
OnMovementChange?.Invoke(input);
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed)
OnJumpPressed?.Invoke();
}
public void OnSpint(InputAction.CallbackContext context)
{
if (context.started)
OnRunPressed?.Invoke(true);
if (context.canceled)
OnRunPressed?.Invoke(false);
}
public void OnAttack(InputAction.CallbackContext context)
{
if (context.performed)
OnAttackPressed?.Invoke();
}
}
}
OnRunPressed 에서 bool 값을 사용해야 합니다.
OnSprint 메서드에서 값 들어오는 것이 시작 되었다면, true 취소 되었다면 false를 Invoke 합니다.
CharacterMovement는 다른 게 없어서 넣지 않고
Player2.cs
using TMPro;
using TopDownView.Player;
using UnityEngine;
namespace TopDownView.SecondPlayer
{
public class Player2 : MonoBehaviour
{
[SerializeField] private CharacterMovement2 movement;
[SerializeField] private PlayerInputSO2 playerInput;
[SerializeField] private TextMeshProUGUI sprintTxt;
private void Start()
{
sprintTxt.text = "Walk";
}
private void Awake()
{
playerInput.OnMovementChange += HandleMovementChange;
playerInput.OnJumpPressed += HandleJumpPressed;
playerInput.OnRunPressed += HandleRunPressed;
playerInput.OnAttackPressed += HandleAttackPressed;
}
private void OnDestroy()
{
playerInput.OnMovementChange -= HandleMovementChange;
playerInput.OnJumpPressed -= HandleJumpPressed;
playerInput.OnRunPressed -= HandleRunPressed;
playerInput.OnAttackPressed -= HandleAttackPressed;
}
private void HandleMovementChange(Vector2 movementnput)
{
movement.SetMovementDirection(movementnput);
}
private void HandleJumpPressed()
{
movement.Jump();
}
public void HandleRunPressed(bool isSprinting)
{
this.movement.SetSprint(isSprinting);
if (isSprinting)
sprintTxt.text = "Running";
else
sprintTxt.text = "Walk";
}
private void HandleAttackPressed()
{
}
}
}
HandleRunPressed 에서 매개변수를 사용합니다.
빨간색 플레이어가 제일 처음 만든 플레이어고, 파란색 플레이어가 홀드로 만든 플레이어 입니다.
'Unity' 카테고리의 다른 글
| 수업 내용 복습 4 (0) | 2025.03.17 |
|---|---|
| 수업 내용 복습 3 (0) | 2025.03.16 |
| 수업 내용 복습 1 (0) | 2025.03.09 |
| 3D 테스트 프로젝트 15 (0) | 2025.03.08 |
| 3D 테스트 프로젝트 14 (0) | 2025.03.07 |