- Home /
XR Interactor not detecting object in my inventory script and idk why.
This script works up until one point. It gets the display item from inventoryItem and that stays inside the inventory bubble I created. Now, whenever a hand collides with the inventory I want the original gameobject to float in place until the player grabs it(I check the rigidbody.iskinematic to see if it's grabbed). the problem is that the XR grab system that unity provides only detects the original item in like a very specific location and angle. the problem is the line that says
invItem.transform.position = gameObject.transform.position;
whenever I take this out it detects like normal, but the object just falls out of the inventory and is still considered held. here is the script:
void Start() { player = GameObject.FindGameObjectWithTag("Player"); headDirection = player.GetComponent().cameraGameObject.transform; }
void Update()
{
transform.position = headDirection.position + (Vector3.down * 0.5f);
InventoryItem inventoryItem = invItem.GetComponent<InventoryItem>();
Rigidbody rb = invItem.GetComponent<Rigidbody>();
XRGrabInteractable grab = invItem.GetComponent<XRGrabInteractable>();
if (touchingInventory && !itemHeld && !rb.isKinematic)
{
Instantiate(inventoryItem.item, transform.position, transform.rotation, transform);
itemHeld = true;
}
if (open && itemHeld && rb.isKinematic)
{
invItem = null;
itemHeld = false;
}
if (!itemHeld)
{
GameObject heldItem = gameObject.transform.GetChild(0).gameObject;
Destroy(heldItem);
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.layer == 9)
{
open = true;
if (itemHeld)
{
invItem.gameObject.SetActive(true);
invItem.transform.position = gameObject.transform.position;
}
}
InventoryItem script = other.gameObject.GetComponent<InventoryItem>();
if (script != null && !itemHeld)
{
touchingInventory = true;
invItem = other.gameObject;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.layer == 9)
{
open = false;
if (itemHeld)
{
invItem.gameObject.SetActive(false);
}
}
InventoryItem script = other.gameObject.GetComponent<InventoryItem>();
if (script != null && !itemHeld)
{
touchingInventory = false;
}
}
}
Your answer
Follow this Question
Related Questions
Raycast hit ignores everything except for the terrain 1 Answer
Raycast not detecting a hit from far away, it only detects when close. 0 Answers
Raycast and actions. 1 Answer
Trying to cast a ray towards a canvas but an invisible copy(?) of the canvas is blocking the way. 1 Answer
Raycast not detecting NavMeshAgent 1 Answer