- Home /
Input System Right Click and Move
Hi! I am making a 2D digital board game and adding in some player controls for camera control using the new Unity Input System (version 2020.2.6f1). For mouse and keyboard setup, I am able to measure a click and a hold (using an Interaction), but I can't seem to combine the hold of left, right, or middle button click in combination with the mouse movement. I tried to make a "Add Button with One Modifier Composite" or "Two Modifier Composite" to an Axis, but that just combines two or three buttons together. I need something that combines the Vector2 delta of a mouse with a mouse or keyboard button click (and hold).
Right now, I have a separate action for the mouse movements to set a state and then one for pointer moves to do something based on state.
Is my approach the right approach or is a Vector2 with a button modifier exist in Unity (or something like it)?
Here is my early input code:
using UnityEngine;
using UnityEngine.InputSystem;
public class HumanPlayerInput : MonoBehaviour
{
[SerializeField]
Player HumanPlayer;
[SerializeField]
Camera PlayerCamera;
PointerChangeState CameraState;
void OnEnable()
{
CameraState = PointerChangeState.None;
if(PlayerCamera = null)
SetPlayerCamera();
}
public void OnSetPan(InputAction.CallbackContext context) => SetPointerState(context.ReadValueAsButton(), PointerChangeState.Pan);
public void OnSetDrag(InputAction.CallbackContext context) => SetPointerState(context.ReadValueAsButton(), PointerChangeState.Drag);
public void OnSetRotate(InputAction.CallbackContext context) => SetPointerState(context.ReadValueAsButton(), PointerChangeState.Rotate);
public void OnPan(InputAction.CallbackContext context) => PanCamera(context.ReadValue<Vector2>());
public void OnPointerMovement(InputAction.CallbackContext context)
{
switch(CameraState)
{
case PointerChangeState.Drag:
break;
case PointerChangeState.Pan:
PanCamera(context.ReadValue<Vector2>());
break;
case PointerChangeState.Rotate:
break;
default:
break;
}
}
public void OnZoom(InputAction.CallbackContext context) => PlayerCamera.orthographicSize = context.ReadValue<float>() * 100f;
void PanCamera(Vector2 changeInPan) => PlayerCamera.transform.Translate(changeInPan * 500f);
void SetPointerState(bool isButtonPressed, PointerChangeState stateToEnable) => CameraState = isButtonPressed ? stateToEnable : PointerChangeState.None;
void SetPlayerCamera()
{
var playerCameraTransform = transform.Find("PlayerCamera");
if(playerCameraTransform == null)
PlayerCamera = Camera.main;
else
{
PlayerCamera = playerCameraTransform.GetComponent<Camera>();
if(PlayerCamera == null)
PlayerCamera = Camera.main;
}
}
}
Your answer
