using UnityEngine;
public class PickUpSystem : MonoBehaviour
{
public Transform holdParent;
public float range = 3f, throwForce = 400f;
private GameObject heldObj;
private Collider playerCollider;
void Start()
{
// Находим коллайдер самого игрока при старте
playerCollider = GetComponent<Collider>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && heldObj == null)
{
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, range) && hit.transform.CompareTag("PickUp"))
{
heldObj = hit.transform.gameObject;
// Игнорируем столкновения между игроком и поднятым предметом
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.SetPositionAndRotation(holdParent.position, holdParent.rotation);
heldObj.transform.parent = holdParent;
if (heldObj.TryGetComponent(out PhysicalFlashlight f)) f.isGrabbed = true;
}
}
else if (Input.GetKeyDown(KeyCode.G) && heldObj != null)
{
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;
}
}
}