117 lines
3.4 KiB
C#
117 lines
3.4 KiB
C#
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 = "";
|
|
|
|
[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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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<Rigidbody2D>(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<Rigidbody2D>(out Rigidbody2D rb))
|
|
{
|
|
rb.bodyType = RigidbodyType2D.Dynamic;
|
|
rb.gravityScale = 1;
|
|
}
|
|
Collider2D col = heldObject.GetComponent<Collider2D>();
|
|
if (col != null) col.enabled = true;
|
|
|
|
// Unparent
|
|
heldObject.transform.SetParent(null);
|
|
heldObject = null;
|
|
}
|
|
}
|