Merge pull request 'Fixing players so that game pads only control one character' (#24) from 4-player-movement-fix into main
Reviewed-on: admin/GGJ2026#24 Reviewed-by: Ramdar <[email protected]>
This commit is contained in:
@@ -199,6 +199,7 @@ MonoBehaviour:
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
controlSchemeName: Keyboard&Mouse
|
||||
gamepadIndex: -1
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 10
|
||||
groundMask:
|
||||
@@ -221,6 +222,7 @@ MonoBehaviour:
|
||||
actionMapName: Player
|
||||
interactActionName: Crouch
|
||||
controlSchemeName: Keyboard&Mouse
|
||||
gamepadIndex: -1
|
||||
holdPoint: {fileID: 5569302833446856119}
|
||||
pickupRange: 1
|
||||
--- !u!1 &6898135930931977916
|
||||
|
||||
@@ -119,6 +119,103 @@ NavMeshSettings:
|
||||
debug:
|
||||
m_Flags: 0
|
||||
m_NavMeshData: {fileID: 0}
|
||||
--- !u!1001 &29258076
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Modification:
|
||||
serializedVersion: 3
|
||||
m_TransformParent: {fileID: 0}
|
||||
m_Modifications:
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: 1.13
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
value: -2.33
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.w
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 3
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: interactActionName
|
||||
value: Interact
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3937090937419967315, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_Name
|
||||
value: Player (4)
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: moveSpeed
|
||||
value: 8
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: jumpImpulse
|
||||
value: 10
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 3
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 8276105153445234536, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_GravityScale
|
||||
value: 2
|
||||
objectReference: {fileID: 0}
|
||||
m_RemovedComponents: []
|
||||
m_RemovedGameObjects: []
|
||||
m_AddedGameObjects: []
|
||||
m_AddedComponents: []
|
||||
m_SourcePrefab: {fileID: 100100000, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
--- !u!1 &375061914
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -188,6 +285,103 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::ScoreManager
|
||||
Score: 0
|
||||
--- !u!1001 &377513953
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Modification:
|
||||
serializedVersion: 3
|
||||
m_TransformParent: {fileID: 0}
|
||||
m_Modifications:
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: 0.43
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
value: -2.37
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.w
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalRotation.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: interactActionName
|
||||
value: Interact
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3937090937419967315, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_Name
|
||||
value: Player (3)
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: moveSpeed
|
||||
value: 8
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: jumpImpulse
|
||||
value: 10
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 8276105153445234536, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_GravityScale
|
||||
value: 2
|
||||
objectReference: {fileID: 0}
|
||||
m_RemovedComponents: []
|
||||
m_RemovedGameObjects: []
|
||||
m_AddedGameObjects: []
|
||||
m_AddedComponents: []
|
||||
m_SourcePrefab: {fileID: 100100000, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
--- !u!1 &418512664
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -1071,11 +1265,11 @@ PrefabInstance:
|
||||
m_Modifications:
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: -2.6910105
|
||||
value: -1.01
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
value: -3.2443483
|
||||
value: -2.36
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
@@ -1109,6 +1303,14 @@ PrefabInstance:
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: interactActionName
|
||||
value: Interact
|
||||
@@ -1117,6 +1319,14 @@ PrefabInstance:
|
||||
propertyPath: m_Name
|
||||
value: Player (1)
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 8276105153445234536, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_GravityScale
|
||||
value: 2
|
||||
@@ -1485,6 +1695,7 @@ MonoBehaviour:
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
controlSchemeName:
|
||||
gamepadIndex: -1
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 8
|
||||
groundMask:
|
||||
@@ -2056,11 +2267,11 @@ PrefabInstance:
|
||||
m_Modifications:
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: 2.3946345
|
||||
value: -0.37
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
value: -3.3612046
|
||||
value: -2.36
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3009946143020725610, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
@@ -2094,6 +2305,10 @@ PrefabInstance:
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
@@ -2118,6 +2333,10 @@ PrefabInstance:
|
||||
propertyPath: jumpImpulse
|
||||
value: 10
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: actionMapName
|
||||
value: Player2
|
||||
@@ -2603,4 +2822,6 @@ SceneRoots:
|
||||
- {fileID: 1759124604}
|
||||
- {fileID: 792686806}
|
||||
- {fileID: 1685410085}
|
||||
- {fileID: 377513953}
|
||||
- {fileID: 29258076}
|
||||
- {fileID: 375061916}
|
||||
|
||||
@@ -467,6 +467,7 @@ MonoBehaviour:
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
controlSchemeName: Gamepad
|
||||
gamepadIndex: 0
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 8
|
||||
groundMask:
|
||||
@@ -670,6 +671,7 @@ MonoBehaviour:
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
controlSchemeName: Gamepad
|
||||
gamepadIndex: 1
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 8
|
||||
groundMask:
|
||||
@@ -873,6 +875,7 @@ MonoBehaviour:
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
controlSchemeName: Gamepad
|
||||
gamepadIndex: 2
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 8
|
||||
groundMask:
|
||||
@@ -1072,10 +1075,30 @@ PrefabInstance:
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 3
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gampad
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3659140714710275197, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: interactActionName
|
||||
value: Interact
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3937090937419967315, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: m_Name
|
||||
value: Player
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: gamepadIndex
|
||||
value: 3
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7763498473808654306, guid: 414ae949bae840c4589b38dd91c715e0, type: 3}
|
||||
propertyPath: controlSchemeName
|
||||
value: Gamepad
|
||||
objectReference: {fileID: 0}
|
||||
m_RemovedComponents: []
|
||||
m_RemovedGameObjects: []
|
||||
m_AddedGameObjects: []
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
@@ -9,6 +10,8 @@ public class PlayerPickupController : MonoBehaviour
|
||||
private InputActionAsset runtimeActions;
|
||||
private InputAction interactAction;
|
||||
[SerializeField] private string controlSchemeName = "";
|
||||
[SerializeField] private int gamepadIndex = -1;
|
||||
private bool deviceFilterEnabled;
|
||||
|
||||
[Header("Setup")]
|
||||
public Transform holdPoint; // Empty GameObject above head
|
||||
@@ -29,6 +32,9 @@ public class PlayerPickupController : MonoBehaviour
|
||||
{
|
||||
runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName);
|
||||
}
|
||||
deviceFilterEnabled = ShouldFilterDevices();
|
||||
ApplyDeviceFilter();
|
||||
InputSystem.onDeviceChange += HandleDeviceChange;
|
||||
|
||||
var map = runtimeActions.FindActionMap(actionMapName, true);
|
||||
interactAction = map.FindAction(interactActionName, true);
|
||||
@@ -53,6 +59,8 @@ public class PlayerPickupController : MonoBehaviour
|
||||
runtimeActions.Disable();
|
||||
runtimeActions = null;
|
||||
}
|
||||
|
||||
InputSystem.onDeviceChange -= HandleDeviceChange;
|
||||
}
|
||||
|
||||
private void OnInteractPerformed(InputAction.CallbackContext context)
|
||||
@@ -113,4 +121,82 @@ public class PlayerPickupController : MonoBehaviour
|
||||
heldObject.transform.SetParent(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.InputSystem;
|
||||
|
||||
@@ -10,6 +11,7 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
[SerializeField] private string moveActionName = "Move";
|
||||
[SerializeField] private string jumpActionName = "Jump";
|
||||
[SerializeField] private string controlSchemeName = "";
|
||||
[SerializeField] private int gamepadIndex = -1;
|
||||
[SerializeField] private float moveSpeed = 6f;
|
||||
[SerializeField] private float jumpImpulse = 8f;
|
||||
[SerializeField] private LayerMask groundMask = ~0;
|
||||
@@ -20,6 +22,7 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
private InputActionAsset runtimeActions;
|
||||
private InputAction moveAction;
|
||||
private InputAction jumpAction;
|
||||
private bool deviceFilterEnabled;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -40,6 +43,9 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
{
|
||||
runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName);
|
||||
}
|
||||
deviceFilterEnabled = ShouldFilterDevices();
|
||||
ApplyDeviceFilter();
|
||||
InputSystem.onDeviceChange += HandleDeviceChange;
|
||||
|
||||
var map = runtimeActions.FindActionMap(actionMapName, true);
|
||||
moveAction = map.FindAction(moveActionName, true);
|
||||
@@ -68,6 +74,8 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
runtimeActions.Disable();
|
||||
runtimeActions = null;
|
||||
}
|
||||
|
||||
InputSystem.onDeviceChange -= HandleDeviceChange;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
@@ -96,6 +104,84 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
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()
|
||||
{
|
||||
var bounds = bodyCollider.bounds;
|
||||
|
||||
Reference in New Issue
Block a user