using UnityEngine;
public class PickUpSystem : MonoBehaviour
{
[Header("Ссылки и настройки")]
public Transform holdParent;
public GameObject uiPrompt; // Сюда перетащим текст InteractPrompt
public float range = 3f, throwForce = 400f;
private GameObject heldObj;
private Collider playerCollider;
void Start()
{
playerCollider = GetComponent<Collider>();
if (uiPrompt != null) uiPrompt.SetActive(false);
}
void Update()
{
// Если в руках уже что-то есть, подсказку "Е" показывать не нужно
if (heldObj != null)
{
if (uiPrompt != null) uiPrompt.SetActive(false);
// Бросок на кнопку G
if (Input.GetKeyDown(KeyCode.G)) ThrowObject();
return;
}
// Пускаем луч для поиска интерактивных объектов
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, range))
{
// Если нашли объект со скриптом Interactable
if (hit.transform.TryGetComponent(out Interactable interactableObj))
{
if (uiPrompt != null) uiPrompt.SetActive(true); // Показываем надпись [E]
if (Input.GetKeyDown(KeyCode.E))
{
interactableObj.Activate(gameObject); // Активируем предмет
}
}
else
{
if (uiPrompt != null) uiPrompt.SetActive(false); // Прячем надпись
}
}
else
{
if (uiPrompt != null) uiPrompt.SetActive(false); // Прячем надпись, если смотрим в пустоту
}
}
// Метод подбора (вызывается из скрипта Interactable)
public void GrabObject(GameObject pickUpObj)
{
heldObj = pickUpObj;
if (heldObj.TryGetComponent(out Collider objCollider) && playerCollider != null)
{
Physics.IgnoreCollision(playerCollider, objCollider, true);
}
if (heldObj.TryGetComponent(out Rigidbody rb)) { rb.isKinematic = true; rb.useGravity = false; }
heldObj.transform.parent = holdParent;
heldObj.transform.localPosition = Vector3.zero;
heldObj.transform.localRotation = Quaternion.identity;
if (heldObj.TryGetComponent(out PhysicalFlashlight f)) f.isGrabbed = true;
if (uiPrompt != null) uiPrompt.SetActive(false);
}
void ThrowObject()
{
if (heldObj.TryGetComponent(out Rigidbody rb)) { rb.isKinematic = false; rb.useGravity = true; rb.AddForce(Camera.main.transform.forward * throwForce); }
if (heldObj.TryGetComponent(out Collider objCollider) && playerCollider != null)
{
Physics.IgnoreCollision(playerCollider, objCollider, false);
}
heldObj.transform.parent = null;
if (heldObj.TryGetComponent(out PhysicalFlashlight f)) f.isGrabbed = false;
heldObj = null;
}
}