using UnityEngine; public class CartGrabAndHold : MonoBehaviour { public float holdDistance = 1.5f; // Distance in front of player public float moveSpeed = 5f; // How fast the cart follows public float floatHeight = 0.5f; // Height above the ground to simulate wheels private bool isHolding = false; // Whether the cart is currently held private Transform player; // The transform of the player private Rigidbody rb; // Cart's rigidbody void Start() { rb = GetComponent(); } // Called by PlayerGrabber when grabbing public void GrabCart(Transform playerTransform) { isHolding = true; player = playerTransform; rb.isKinematic = true; // Disable physics for smooth movement } // Called by PlayerGrabber when releasing public void ReleaseCart() { isHolding = false; rb.isKinematic = false; // Re-enable physics player = null; } // Used by PlayerGrabber to keep cart following the player public void HoldCart() { if (!isHolding || player == null) return; // Target position is in front of the player on X/Z, floating above ground Vector3 targetPosition = player.position + player.forward * holdDistance; targetPosition.y = floatHeight; // Smoothly move cart toward target transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * moveSpeed); // Face the same direction as the player transform.rotation = Quaternion.LookRotation(player.forward); } public bool IsHolding() { return isHolding; } }