For those attempting to use the Unity's New Input System

  1. Ensure that you create the .inputactions file,
    image

  2. Map whatever button you would like to use,

  3. and Generate the C# class;
    image
    image

  4. 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();
    }

}
6 Likes

Thanks for this! :pray:t3:

1 Like

I had ran into several bugs when using the newer Input System last time I tried it. I might give this course a try using both methods to see which one feels better now. Great job on the script!

Privacy & Terms