diff --git a/Assets/Art/Final Sprites.meta b/Assets/Art/Final Sprites.meta new file mode 100644 index 0000000..8ed993f --- /dev/null +++ b/Assets/Art/Final Sprites.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c837a5f2052e2924f8b35a0fcf565f57 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Art/Final Sprites/Candy.png b/Assets/Art/Final Sprites/Candy.png new file mode 100644 index 0000000..350c723 Binary files /dev/null and b/Assets/Art/Final Sprites/Candy.png differ diff --git a/Assets/Art/Final Sprites/Candy.png.meta b/Assets/Art/Final Sprites/Candy.png.meta new file mode 100644 index 0000000..c20464d --- /dev/null +++ b/Assets/Art/Final Sprites/Candy.png.meta @@ -0,0 +1,143 @@ +fileFormatVersion: 2 +guid: dc90badfe9725d24aacabda7750ad3a4 +TextureImporter: + internalIDToNameTable: + - first: + 213: -3366272070310193754 + second: Candy_0 + externalObjects: {} + serializedVersion: 13 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + flipGreenChannel: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + ignoreMipmapLimit: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 1 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 2 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + swizzle: 50462976 + cookieLightType: 0 + platformSettings: + - serializedVersion: 4 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 4 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + ignorePlatformSupport: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: + - serializedVersion: 2 + name: Candy_0 + rect: + serializedVersion: 2 + x: 122 + y: 215 + width: 151 + height: 79 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + customData: + outline: [] + physicsShape: [] + tessellationDetail: -1 + bones: [] + spriteID: 6a597d723699841d0800000000000000 + internalID: -3366272070310193754 + vertices: [] + indices: + edges: [] + weights: [] + outline: [] + customData: + physicsShape: [] + bones: [] + spriteID: + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spriteCustomMetadata: + entries: [] + nameFileIdTable: + Candy_0: -3366272070310193754 + mipmapLimitGroupName: + pSDRemoveMatte: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index ac0539b..3b73b79 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -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 diff --git a/Assets/Prefabs/Treat.prefab b/Assets/Prefabs/Treat.prefab index c2ac06a..4577bf2 100644 --- a/Assets/Prefabs/Treat.prefab +++ b/Assets/Prefabs/Treat.prefab @@ -83,8 +83,8 @@ SpriteRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_MaskInteraction: 0 - m_Sprite: {fileID: -2413806693520163455, guid: a86470a33a6bf42c4b3595704624658b, type: 3} - m_Color: {r: 0.4018334, g: 0.8113208, b: 0.46312103, a: 1} + m_Sprite: {fileID: -3366272070310193754, guid: dc90badfe9725d24aacabda7750ad3a4, type: 3} + m_Color: {r: 0.8773585, g: 0.26072446, b: 0.8517005, a: 1} m_FlipX: 0 m_FlipY: 0 m_DrawMode: 0 diff --git a/Assets/Scenes/Experiment.unity b/Assets/Scenes/Experiment.unity index 779c1c6..1fb2b9f 100644 --- a/Assets/Scenes/Experiment.unity +++ b/Assets/Scenes/Experiment.unity @@ -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 @@ -2651,5 +2870,7 @@ SceneRoots: - {fileID: 1759124604} - {fileID: 792686806} - {fileID: 1685410085} + - {fileID: 377513953} + - {fileID: 29258076} - {fileID: 375061916} - {fileID: 2100000001} diff --git a/Assets/Scenes/Level1.unity b/Assets/Scenes/Level1.unity index 3beab9e..7a71250 100644 --- a/Assets/Scenes/Level1.unity +++ b/Assets/Scenes/Level1.unity @@ -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: [] diff --git a/Assets/Scripts/PlayerPickupController.cs b/Assets/Scripts/PlayerPickupController.cs index 87d4546..e960e34 100644 --- a/Assets/Scripts/PlayerPickupController.cs +++ b/Assets/Scripts/PlayerPickupController.cs @@ -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) @@ -122,4 +130,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(); + 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; + } } diff --git a/Assets/Scripts/PlayerPlatformerController.cs b/Assets/Scripts/PlayerPlatformerController.cs index 0d580a5..2562486 100644 --- a/Assets/Scripts/PlayerPlatformerController.cs +++ b/Assets/Scripts/PlayerPlatformerController.cs @@ -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(); + 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;