-
Ensure that you create the .inputactions file,
-
Map whatever button you would like to use,
-
and Generate the C# class;
-
Here is the code that I have used with success. Hopefully this is helpful to you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
public class RightClick : MonoBehaviour
{
PlayerInput _playerInput;
Ray _lastRay;
bool _isRightClickPressed;
[SerializeField] Transform _target;
private void Awake()
{
_playerInput = new PlayerInput();
_playerInput.MouseUIControls.RightClick.started += OnRightClick;
_playerInput.MouseUIControls.RightClick.canceled += OnRightClick;
}
void Update()
{
if (_isRightClickPressed)
{
_lastRay = Camera.main.ScreenPointToRay(Input.mousePosition);
}
Debug.DrawRay(_lastRay.origin, _lastRay.direction * 100);
GetComponent<NavMeshAgent>().destination = _target.position;
}
public void OnRightClick(InputAction.CallbackContext context)
{
_isRightClickPressed = context.ReadValueAsButton();
}
private void OnEnable()
{
_playerInput.MouseUIControls.Enable();
}
private void OnDisable()
{
_playerInput.MouseUIControls.Disable();
}
}