Fixing players so that game pads only control one character

This commit is contained in:
hz
2026-01-31 17:45:45 -06:00
parent 0c031b558e
commit 61b79ae4ed
4 changed files with 181 additions and 0 deletions
+2
View File
@@ -199,6 +199,7 @@ MonoBehaviour:
moveActionName: Move moveActionName: Move
jumpActionName: Jump jumpActionName: Jump
controlSchemeName: Keyboard&Mouse controlSchemeName: Keyboard&Mouse
gamepadIndex: -1
moveSpeed: 6 moveSpeed: 6
jumpImpulse: 10 jumpImpulse: 10
groundMask: groundMask:
@@ -221,6 +222,7 @@ MonoBehaviour:
actionMapName: Player actionMapName: Player
interactActionName: Crouch interactActionName: Crouch
controlSchemeName: Keyboard&Mouse controlSchemeName: Keyboard&Mouse
gamepadIndex: -1
holdPoint: {fileID: 5569302833446856119} holdPoint: {fileID: 5569302833446856119}
pickupRange: 1 pickupRange: 1
--- !u!1 &6898135930931977916 --- !u!1 &6898135930931977916
+7
View File
@@ -467,6 +467,7 @@ MonoBehaviour:
moveActionName: Move moveActionName: Move
jumpActionName: Jump jumpActionName: Jump
controlSchemeName: Gamepad controlSchemeName: Gamepad
gamepadIndex: 0
moveSpeed: 6 moveSpeed: 6
jumpImpulse: 8 jumpImpulse: 8
groundMask: groundMask:
@@ -670,6 +671,7 @@ MonoBehaviour:
moveActionName: Move moveActionName: Move
jumpActionName: Jump jumpActionName: Jump
controlSchemeName: Gamepad controlSchemeName: Gamepad
gamepadIndex: 1
moveSpeed: 6 moveSpeed: 6
jumpImpulse: 8 jumpImpulse: 8
groundMask: groundMask:
@@ -873,6 +875,7 @@ MonoBehaviour:
moveActionName: Move moveActionName: Move
jumpActionName: Jump jumpActionName: Jump
controlSchemeName: Gamepad controlSchemeName: Gamepad
gamepadIndex: 2
moveSpeed: 6 moveSpeed: 6
jumpImpulse: 8 jumpImpulse: 8
groundMask: groundMask:
@@ -1072,6 +1075,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
propertyPath: interactActionName
value: Interact
objectReference: {fileID: 0}
- target: {fileID: 3937090937419967315, guid: 414ae949bae840c4589b38dd91c715e0, type: 3} - target: {fileID: 3937090937419967315, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
propertyPath: m_Name propertyPath: m_Name
value: Player value: Player
+86
View File
@@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
@@ -9,6 +10,8 @@ public class PlayerPickupController : MonoBehaviour
private InputActionAsset runtimeActions; private InputActionAsset runtimeActions;
private InputAction interactAction; private InputAction interactAction;
[SerializeField] private string controlSchemeName = ""; [SerializeField] private string controlSchemeName = "";
[SerializeField] private int gamepadIndex = -1;
private bool deviceFilterEnabled;
[Header("Setup")] [Header("Setup")]
public Transform holdPoint; // Empty GameObject above head public Transform holdPoint; // Empty GameObject above head
@@ -29,6 +32,9 @@ public class PlayerPickupController : MonoBehaviour
{ {
runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName); runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName);
} }
deviceFilterEnabled = ShouldFilterDevices();
ApplyDeviceFilter();
InputSystem.onDeviceChange += HandleDeviceChange;
var map = runtimeActions.FindActionMap(actionMapName, true); var map = runtimeActions.FindActionMap(actionMapName, true);
interactAction = map.FindAction(interactActionName, true); interactAction = map.FindAction(interactActionName, true);
@@ -53,6 +59,8 @@ public class PlayerPickupController : MonoBehaviour
runtimeActions.Disable(); runtimeActions.Disable();
runtimeActions = null; runtimeActions = null;
} }
InputSystem.onDeviceChange -= HandleDeviceChange;
} }
private void OnInteractPerformed(InputAction.CallbackContext context) private void OnInteractPerformed(InputAction.CallbackContext context)
@@ -113,4 +121,82 @@ public class PlayerPickupController : MonoBehaviour
heldObject.transform.SetParent(null); heldObject.transform.SetParent(null);
heldObject = null; heldObject = null;
} }
private bool ShouldFilterDevices()
{
return string.Equals(controlSchemeName, "Gamepad", StringComparison.OrdinalIgnoreCase) || gamepadIndex >= 0;
}
private void ApplyDeviceFilter()
{
if (!deviceFilterEnabled || runtimeActions == null)
{
return;
}
var targetIndex = ResolveGamepadIndex();
if (targetIndex < 0)
{
return;
}
if (Gamepad.all.Count <= targetIndex)
{
runtimeActions.devices = Array.Empty<InputDevice>();
return;
}
runtimeActions.devices = new InputDevice[] { Gamepad.all[targetIndex] };
}
private void HandleDeviceChange(InputDevice device, InputDeviceChange change)
{
if (!deviceFilterEnabled)
{
return;
}
if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed || change == InputDeviceChange.Reconnected)
{
ApplyDeviceFilter();
}
}
private int ResolveGamepadIndex()
{
if (gamepadIndex >= 0)
{
return gamepadIndex;
}
if (string.IsNullOrWhiteSpace(actionMapName))
{
return -1;
}
if (string.Equals(actionMapName, "Player", StringComparison.OrdinalIgnoreCase))
{
return 0;
}
var end = actionMapName.Length - 1;
var start = end;
while (start >= 0 && char.IsDigit(actionMapName[start]))
{
start--;
}
start++;
if (start > end)
{
return -1;
}
if (!int.TryParse(actionMapName.Substring(start, end - start + 1), out var parsed))
{
return -1;
}
return parsed > 0 ? parsed - 1 : -1;
}
} }
@@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
@@ -10,6 +11,7 @@ public class PlayerPlatformerController : MonoBehaviour
[SerializeField] private string moveActionName = "Move"; [SerializeField] private string moveActionName = "Move";
[SerializeField] private string jumpActionName = "Jump"; [SerializeField] private string jumpActionName = "Jump";
[SerializeField] private string controlSchemeName = ""; [SerializeField] private string controlSchemeName = "";
[SerializeField] private int gamepadIndex = -1;
[SerializeField] private float moveSpeed = 6f; [SerializeField] private float moveSpeed = 6f;
[SerializeField] private float jumpImpulse = 8f; [SerializeField] private float jumpImpulse = 8f;
[SerializeField] private LayerMask groundMask = ~0; [SerializeField] private LayerMask groundMask = ~0;
@@ -20,6 +22,7 @@ public class PlayerPlatformerController : MonoBehaviour
private InputActionAsset runtimeActions; private InputActionAsset runtimeActions;
private InputAction moveAction; private InputAction moveAction;
private InputAction jumpAction; private InputAction jumpAction;
private bool deviceFilterEnabled;
private void Awake() private void Awake()
{ {
@@ -40,6 +43,9 @@ public class PlayerPlatformerController : MonoBehaviour
{ {
runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName); runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName);
} }
deviceFilterEnabled = ShouldFilterDevices();
ApplyDeviceFilter();
InputSystem.onDeviceChange += HandleDeviceChange;
var map = runtimeActions.FindActionMap(actionMapName, true); var map = runtimeActions.FindActionMap(actionMapName, true);
moveAction = map.FindAction(moveActionName, true); moveAction = map.FindAction(moveActionName, true);
@@ -68,6 +74,8 @@ public class PlayerPlatformerController : MonoBehaviour
runtimeActions.Disable(); runtimeActions.Disable();
runtimeActions = null; runtimeActions = null;
} }
InputSystem.onDeviceChange -= HandleDeviceChange;
} }
private void FixedUpdate() private void FixedUpdate()
@@ -96,6 +104,84 @@ public class PlayerPlatformerController : MonoBehaviour
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse); body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
} }
private bool ShouldFilterDevices()
{
return string.Equals(controlSchemeName, "Gamepad", StringComparison.OrdinalIgnoreCase) || gamepadIndex >= 0;
}
private void ApplyDeviceFilter()
{
if (!deviceFilterEnabled || runtimeActions == null)
{
return;
}
var targetIndex = ResolveGamepadIndex();
if (targetIndex < 0)
{
return;
}
if (Gamepad.all.Count <= targetIndex)
{
runtimeActions.devices = Array.Empty<InputDevice>();
return;
}
runtimeActions.devices = new InputDevice[] { Gamepad.all[targetIndex] };
}
private void HandleDeviceChange(InputDevice device, InputDeviceChange change)
{
if (!deviceFilterEnabled)
{
return;
}
if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed || change == InputDeviceChange.Reconnected)
{
ApplyDeviceFilter();
}
}
private int ResolveGamepadIndex()
{
if (gamepadIndex >= 0)
{
return gamepadIndex;
}
if (string.IsNullOrWhiteSpace(actionMapName))
{
return -1;
}
if (string.Equals(actionMapName, "Player", StringComparison.OrdinalIgnoreCase))
{
return 0;
}
var end = actionMapName.Length - 1;
var start = end;
while (start >= 0 && char.IsDigit(actionMapName[start]))
{
start--;
}
start++;
if (start > end)
{
return -1;
}
if (!int.TryParse(actionMapName.Substring(start, end - start + 1), out var parsed))
{
return -1;
}
return parsed > 0 ? parsed - 1 : -1;
}
private bool IsGrounded() private bool IsGrounded()
{ {
var bounds = bodyCollider.bounds; var bounds = bodyCollider.bounds;