Input System, Player Controller and Mover layers

Since I gave myself the challenge of using the new Input System for this course, this is my scripts arrangement.

I have separated out a class “InputActionHandler” for handling all input related stuff, the mouse position inputs, enabling/disabling input actions etc.

Not sure if I’m doing it right though but it works so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

//Handles Player Inputs with Input System
//InputAction Asset instance, Enable/Disable Input Actions
public class InputActionHandler : MonoBehaviour
{
    public PlayerInputAsset playerInputAsset;
    public InputAction pointer;

    public Vector2 mousePosition { get; private set; }

    void Awake()
    {
        playerInputAsset = new PlayerInputAsset();
        pointer = playerInputAsset.Player.Pointer;
    }

    void Update()
    {
        mousePosition = pointer.ReadValue<Vector2>();   //tracks mouse pos for PlayerController actions
    }

    void OnEnable()
    {
        playerInputAsset.Player.MouseLeftClick.Enable();
        playerInputAsset.Player.MouseLeftClickHold.Enable();
        pointer.Enable();
    }


    void OnDisable()
    {
        playerInputAsset.Player.MouseLeftClick.Disable();
        playerInputAsset.Player.MouseLeftClickHold.Disable();
        pointer.Disable();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

//Player control actions
//Requires Input module (InputActionHandler) and action modules (Mover etc)
[RequireComponent(typeof(InputActionHandler))]
[RequireComponent(typeof(Mover))]
public class PlayerController : MonoBehaviour
{
    InputActionHandler inputActionHandler;
    PlayerInputAsset.PlayerActions playerActions;

    Camera cam;
    Mover mover;

    bool isLeftMouseHold;

    void Awake()
    {
        cam = FindObjectOfType<Camera>();
        mover = GetComponent<Mover>();

        inputActionHandler = GetComponent<InputActionHandler>();
        playerActions = inputActionHandler.playerInputAsset.Player;
    }

    //Subscribe methods to input action events
    void OnEnable()
    {
        playerActions.MouseLeftClick.performed += MoveToPointed;

        playerActions.MouseLeftClickHold.performed += ctx => isLeftMouseHold = true;
        playerActions.MouseLeftClickHold.canceled += ctx => isLeftMouseHold = false;
    }


    //Unsubscribe methods to input action events
    void OnDisable()
    {
        playerActions.MouseLeftClick.performed -= MoveToPointed;

        playerActions.MouseLeftClickHold.performed -= ctx => isLeftMouseHold = true;
        playerActions.MouseLeftClickHold.canceled -= ctx => isLeftMouseHold = false;
    }

    void Update()
    {
        HoldClickControl();
    }

    //Checks if LMB is held for hold click movement
    void HoldClickControl()
    {
        if (isLeftMouseHold)
        {
            MoveToPointed();
        }
    }

    //Moves gameObject to mouse pointed position
    public void MoveToPointed(InputAction.CallbackContext obj)
    {
        Vector2 mousePos = inputActionHandler.mousePosition;
        RaycastHit hit;
        bool hasHit;

        Ray ray = cam.ScreenPointToRay(mousePos);
        hasHit = Physics.Raycast(ray, out hit);
        if (hasHit)
        {
            mover.MoveTo(hit.point);
        }
    }

    //Overloaded method of above for HoldClickControl logic
    public void MoveToPointed()
    {
        Vector2 mousePos = inputActionHandler.mousePosition;
        RaycastHit hit;
        bool hasHit;

        Ray ray = cam.ScreenPointToRay(mousePos);
        hasHit = Physics.Raycast(ray, out hit);
        if (hasHit)
        {
            mover.MoveTo(hit.point);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Mover : MonoBehaviour
{
    NavMeshAgent agent;

    void Awake()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    public void MoveTo(Vector3 point)
    {
        agent.SetDestination(point);
    }
}

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms