using UnityEngine; public class CartPickup : MonoBehaviour { public float pickupRange = 3f; public Transform holdPoint; // An empty GameObject in front of player private GameObject heldObject; private bool isHolding = false; void Update() { if (Input.GetKeyDown(KeyCode.E)) { if (isHolding) { DropObject(); } else { Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); if (Physics.Raycast(ray, out RaycastHit hit, pickupRange)) { if (hit.collider.CompareTag("Cart")) { PickupObject(hit.collider.gameObject); } } } } // Optional: Keep the cart tightly in position if (isHolding && heldObject != null) { heldObject.transform.position = holdPoint.position; heldObject.transform.rotation = holdPoint.rotation; } } void PickupObject(GameObject obj) { heldObject = obj; isHolding = true; obj.GetComponent().isKinematic = true; obj.transform.SetParent(holdPoint); obj.transform.localPosition = Vector3.zero; } void DropObject() { if (heldObject != null) { heldObject.transform.SetParent(null); heldObject.GetComponent().isKinematic = false; heldObject = null; } isHolding = false; } }