How can I transfer "OnTriggerEnter (Collider other)"?

  Uncategorised

How can I transfer "OnTriggerEnter (Collider other)" to the second script to make everything work?
I wanted him to use an "E" to pick up things, so I used raycasts but now I don’t know what to do next.
Thank you in advance.

public GameObject itemPickedUp;
private bool itemAdded;
private bool pickUpAllowed;

public void OnTriggerEnter(Collider other)
    {
        if (other.tag=="Item")
        {
            print("Colliding!");
            itemPickedUp = other.gameObject;
            AddItem(itemPickedUp);
            pickUpAllowed = true;
        }
    }

    public void OnTriggerExit(Collider other)
    {
        if (other.tag == "Item")
        {
            itemAdded = false;
            pickUpAllowed = false;
        }
    }

Second script

private Inventory item;
private Player player;

public GameObject interactCanvas;  
void Update()
{
    Ray ray = FPSCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
    RaycastHit hitInfo;

    if (Physics.Raycast(ray, out hitInfo, IteractRange))
    {


    interactCanvas.SetActive(true);
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (hitInfo.collider.tag == "Item")
            {
                item = hitInfo.collider.GetComponent<Inventory>();

                item.AddItem(item.itemPickedUp);
            }
        }
    }
    else
    {
        interactCanvas.SetActive(false);
    }
}

Thanks!

Source: Visual Studio Questions

LEAVE A COMMENT