How do i change control scheme and pair with devies WITHOUT PlayerInput
like how do i make it so input from different devies control the characters instead of the same character
there is 2 control schemes
keyboard
controller
i tried to change it like this but it doesnt work
if (PlayerInputType == PlayerHandler.InputType.keyboard) {
playerControls.bindingMask = InputBinding.MaskByGroup("keyboard");
playerControls.devices = new[] { Keyboard.current };
}
else if (PlayerInputType == PlayerHandler.InputType.controller) {
playerControls.bindingMask = InputBinding.MaskByGroup("controller");
playerControls.devices = new[] { Gamepad.all[InputGamepadIndex] };
}
Well, I assume playerControls is a reference to your InputActionAsset or maybe InputActionCollection, I am not sure.
Can you share more of what doesn’t work exactly.
Maybe, you forgot to assign the bindings to different control schemes in your asset.
Or maybe you forgot to enable the actions, by default they are disabled. action.Enable();
Side note: I am not sure why you don’t want to use the PlayerInput, if you have different gameObjects/characters and you want them to be controlled using different devices, then you can have a PlayerInput for each them with different default control schemes and make sure to disable automatic switching of control schemes. This is much easier in my opinion. However, I am not entirely sure of your use case, if you can share more about your setup and components, I will be able to help.
The PlayerControls is a generated c# class
everything works but All the devices control the same character and ii want to to be controlled by different devices
i use my own way to spawn the characters and it tells if playing on keyboard or gamepad so InputGamepadIndex is right
there is InputAction variables and i use .WasPressedThisFrame() to detect inputs
i tried to do it like this too but it also doesnt work
if (PlayerInputType == PlayerHandler.InputType.keyboard) {
InputUser playerUser = InputUser.PerformPairingWithDevice(Keyboard.current);
InputUser.PerformPairingWithDevice(Mouse.current, user: playerUser);
playerUser.AssociateActionsWithUser(playerControls);
playerUser.ActivateControlScheme("keyboard");
playerControls.Enable();
}
else if (PlayerInputType == PlayerHandler.InputType.controller) {
InputUser playerUser = InputUser.PerformPairingWithDevice(Gamepad.all[InputGamepadIndex]);
playerUser.AssociateActionsWithUser(playerControls);
playerUser.ActivateControlScheme("controller");
playerControls.Enable();
}
heres the important parts i think
public PlayerControlAction playerControls;
//action thing
private InputAction actionMovement;
private InputAction actionJump;
private InputAction actionAiming;
private InputAction actionPrimary;
private InputAction actionSecondary;
private InputAction actionThird;
private InputAction actionInteract;
// InputData
public Vector2 movementInput { get; private set; }
public bool jumpTriggering { get; private set; }
public Vector2 aimingInput { get; private set; }
public bool primaryTriggering { get; private set; }
public bool secondaryTriggering { get; private set; }
public bool thirdTriggering { get; private set; }
public bool interactTriggering { get; private set; }
private void Awake() {
playerControls = new PlayerControlAction();
Setup();
}
private void Update() {
ControlCharacter();
}
private void OnEnable() {
playerControls?.Enable();
actionMovement?.Enable();
actionJump?.Enable();
actionAiming?.Enable();
actionPrimary?.Enable();
actionSecondary?.Enable();
actionThird?.Enable();
actionInteract?.Enable();
}
private void OnDisable() {
playerControls?.Disable();
actionMovement?.Disable();
actionJump?.Disable();
actionAiming?.Disable();
actionPrimary?.Disable();
actionSecondary?.Disable();
actionThird?.Disable();
actionInteract?.Disable();
}
public void Setup() {
if (playerControls != null) {
if (PlayerInputType == PlayerHandler.InputType.keyboard) {
InputUser playerUser = InputUser.PerformPairingWithDevice(Keyboard.current);
InputUser.PerformPairingWithDevice(Mouse.current, user: playerUser);
playerUser.AssociateActionsWithUser(playerControls);
playerUser.ActivateControlScheme("keyboard");
playerControls.Enable();
}
else if (PlayerInputType == PlayerHandler.InputType.controller) {
InputUser playerUser = InputUser.PerformPairingWithDevice(Gamepad.all[InputGamepadIndex]);
playerUser.AssociateActionsWithUser(playerControls);
playerUser.ActivateControlScheme("gamepad");
playerControls.Enable();
}
if (MainGameManager.singleton.gameDimension == MainGameManager.dimension.CG_3D) {
actionMovement = playerControls.player3D.movement;
actionJump = playerControls.player3D.jump;
actionAiming = playerControls.player3D.aiming;
actionPrimary = playerControls.player3D.primary;
actionSecondary = playerControls.player3D.secondary;
actionThird = playerControls.player3D.third;
actionInteract = playerControls.player3D.interact;
}
else if (MainGameManager.singleton.gameDimension == MainGameManager.dimension.CG_2D) {
actionMovement = playerControls.player2D.movement;
actionJump = playerControls.player2D.jump;
actionAiming = playerControls.player2D.aiming;
actionPrimary = playerControls.player2D.primary;
actionSecondary = playerControls.player2D.secondary;
actionThird = playerControls.player2D.third;
actionInteract = playerControls.player2D.interact;
}
else if (MainGameManager.singleton.gameDimension == MainGameManager.dimension.CG_3DBut2D) {
actionMovement = playerControls.player2D.movement;
actionJump = playerControls.player2D.jump;
actionAiming = playerControls.player2D.aiming;
actionPrimary = playerControls.player2D.primary;
actionSecondary = playerControls.player2D.secondary;
actionThird = playerControls.player2D.third;
actionInteract = playerControls.player2D.interact;
}
RegisterInputActions();
}
}
void RegisterInputActions() {
actionMovement.performed += context => movementInput = context.ReadValue<Vector2>();
actionMovement.canceled += context => movementInput = Vector2.zero;
actionJump.performed += context => jumpTriggering = true;
actionJump.canceled += context => jumpTriggering = false;
actionAiming.performed += context => aimingInput = context.ReadValue<Vector2>();
actionAiming.canceled += context => aimingInput = Vector2.zero;
actionPrimary.performed += context => primaryTriggering = true;
actionPrimary.canceled += context => primaryTriggering = false;
actionSecondary.performed += context => secondaryTriggering = true;
actionSecondary.canceled += context => secondaryTriggering = false;
actionThird.performed += context => thirdTriggering = true;
actionThird.canceled += context => thirdTriggering = false;
actionInteract.performed += context => interactTriggering = true;
actionInteract.canceled += context => interactTriggering = false;
}
ok i figured it out!
this one was very helpful
I followed Koru's lead and managed to have local multiplayer without using PlayerInput or PlayerInputManager.
Here is the relevant code for my mutliplayer Tetris clone.
To see the code in context (in a working project) check out the source.
Keep in mind that the Tetris class here basically is the player.
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Users;
public class Global : MonoBehaviour
{
…
Can I ask, how did you end up switching schemes/devices?
Thanks!