Загрузка данных


using UnityEngine;

public class Interactor : MonoBehaviour
{
    public LayerMask layerMask;
    public Transform handPoint;
    public float throwForce = 5f;
    public bool hasGrabbableObject = false;
    private GameObject grabbableObject;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 inputMouse = new Vector3(
                Screen.width / 2f,
                Screen.height / 2f,
                0
            );

            Ray ray = Camera.main.ScreenPointToRay(inputMouse);

            if (Physics.Raycast(ray, out RaycastHit hit, 10f, layerMask))
            {
                GameObject obj = hit.collider.gameObject;
                Interactable interactable = obj.GetComponent<Interactable>();
                if (interactable != null && !hasGrabbableObject)
                {
                    grabbableObject = obj;
                    interactable.isGrabbable = true;
                    Debug.Log(obj.name);
                    Rigidbody rb = obj.GetComponent<Rigidbody>();
                    rb.isKinematic = true;
                    if (interactable.grabPoint == null)
                    {
                        obj.transform.position = handPoint.position;
                        obj.transform.rotation = handPoint.rotation;
                        obj.transform.SetParent(handPoint);
                    }
                    else
                    {
                        obj.transform.SetParent(handPoint);
                        obj.transform.localPosition = -interactable.grabPoint.localPosition;
                        obj.transform.localRotation = Quaternion.Euler(-interactable.grabPoint.localEulerAngles);
                    }

                    Collider[] colliders = obj.GetComponents<Collider>();
                    foreach (Collider collider in colliders)
                    {
                        collider.enabled = false;
                    }

                    hasGrabbableObject = true;
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.G) && hasGrabbableObject)
        {
            grabbableObject.transform.SetParent(null);
            Collider[] colliders = grabbableObject.GetComponents<Collider>();
            foreach (Collider collider in colliders)
            {
                collider.enabled = true;
            }
            Rigidbody rb = grabbableObject.GetComponent<Rigidbody>();
            rb.isKinematic = false;
            rb.AddForce(Camera.main.transform.forward * throwForce, ForceMode.Impulse);
            grabbableObject = null;
            hasGrabbableObject = false;
            grabbableObject.GetComponent<Interactable>().isGrabbable = false;
        }
    }
}