首先创建字段和对输入操作映射的引用。
[SerializeField]LayerMask _mask;
GameActions input;
Vector3 movetowards;
Vector3 targetPosition;
float stopingDistance;
接下来,我启用了玩家动作图并调用了执行的事件。
private void Awake()
{
input = new GameActions();
}
private void OnEnable()
{
input.Player.Enable();
input.Player.ChangeColor.performed += MoveObject;
}
private void MoveObject(InputAction.CallbackContext context)
{
Ray _ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(_ray, out RaycastHit _hit, Mathf.Infinity, _mask))
{
mousePosition = _hit.point;
Vector3 currentPosition = transform.position;
movetowards = (mousePosition - currentPosition).normalized;
}
}