using System; using UnityEngine; using UnityEngine.InputSystem; public class PlayerPickupController : MonoBehaviour { [SerializeField] private InputActionAsset inputActions; [SerializeField] private string actionMapName = "Player"; [SerializeField] private string interactActionName = "Interact"; 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 public float pickupRange = 2.0f; private GameObject heldObject; private void OnEnable() { if (inputActions == null) { Debug.LogError("PlayerPickupController missing InputActionAsset reference.", this); return; } runtimeActions = Instantiate(inputActions); if (!string.IsNullOrWhiteSpace(controlSchemeName)) { runtimeActions.bindingMask = InputBinding.MaskByGroup(controlSchemeName); } deviceFilterEnabled = ShouldFilterDevices(); ApplyDeviceFilter(); InputSystem.onDeviceChange += HandleDeviceChange; var map = runtimeActions.FindActionMap(actionMapName, true); interactAction = map.FindAction(interactActionName, true); if (interactAction != null) { interactAction.Enable(); interactAction.performed += OnInteractPerformed; } } private void OnDisable() { if (interactAction != null) { interactAction.performed -= OnInteractPerformed; interactAction.Disable(); } if (runtimeActions != null) { runtimeActions.Disable(); runtimeActions = null; } InputSystem.onDeviceChange -= HandleDeviceChange; } private void OnInteractPerformed(InputAction.CallbackContext context) { Debug.Log("Interact action performed"); if(heldObject != null) { Debug.Log("Dropping held object"); DropObject(); return; } TryPickUp(); } void TryPickUp() { // Find all colliders within range Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, pickupRange); Debug.Log(colliders.Length + " colliders found in range"); foreach (var col in colliders) { if (col.CompareTag("Interactable")) { heldObject = col.gameObject; // Parent the object to the HoldPoint and reset its local position heldObject.transform.SetParent(holdPoint); heldObject.transform.localPosition = Vector3.zero; heldObject.transform.localRotation = Quaternion.identity; // Disable physics so it doesn't fall or collide with the player if (heldObject.TryGetComponent(out Rigidbody2D rb)) { rb.bodyType = RigidbodyType2D.Kinematic; rb.gravityScale = 0; rb.mass = 0; } if (col != null) col.enabled = false; break; } } } void DropObject() { // Re-enable physics if (heldObject.TryGetComponent(out Rigidbody2D rb)) { rb.bodyType = RigidbodyType2D.Dynamic; rb.gravityScale = 1; } Collider2D col = heldObject.GetComponent(); if (col != null) col.enabled = true; // Unparent 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; } }