// Update is called once per frame void Update() { timer += Time.deltaTime; if (rightPointerObject != null) { previousPointerPos = rightPointerObject.gameObject.transform.position; } waistPosition = myCam.gameObject.transform.position - new Vector3(0, 1, 0); if (Input.GetKeyDown("j")) { Debug.Log("Pressed 'J' key."); Vector3 originPosition = myCam.gameObject.transform.position; Vector3 rayDirection = myCam.gameObject.transform.forward; RaycastHit hit; if (Physics.Raycast(originPosition, rayDirection, out hit, 1)) { GameObject hitObject = hit.collider.gameObject; CollectibleTreasure objectComponent = hitObject.GetComponent <CollectibleTreasure>(); string objectName = objectComponent.getType(); //should do a null reference check here CollectibleTreasure prefab = (CollectibleTreasure)Resources.Load(objectName, typeof(CollectibleTreasure)); if (!prefab) { Debug.Log("Prefab is null."); } addToInventory(prefab); Destroy(hitObject); updateTreasure(); treasureItems[currentTreasureIndex].gameObject.active = true; } //triggerTrap(); } else if (OVRInput.GetDown(OVRInput.RawButton.RHandTrigger)) { Collider[] overlappingThings = Physics.OverlapSphere(rightPointerObject.transform.position, 0.1f, collectiblesMask); if (overlappingThings.Length > 0) { attachGameObjectToAChildGameObject(overlappingThings[0].gameObject, rightPointerObject, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, true); //I'm not bothering to check for nullity because layer mask should ensure I only collect collectibles. thingIGrabbed = overlappingThings[0].gameObject.GetComponent <CollectibleTreasure>(); if ((thingIGrabbed == trapTreasure) && !(trapTriggered)) { triggerTrap(); } } } else if (OVRInput.GetUp(OVRInput.RawButton.RHandTrigger)) { if (!(null == thingIGrabbed)) { letGo(); } } }
void letGo() { detachGameObject(thingIGrabbed.gameObject, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld, AttachmentRule.KeepWorld); simulatePhysics(thingIGrabbed.gameObject, (rightPointerObject.gameObject.transform.position - previousPointerPos) / Time.deltaTime, true); if (canPutInInventory(thingIGrabbed)) { string objectName = thingIGrabbed.getType(); CollectibleTreasure prefab = (CollectibleTreasure)Resources.Load(objectName, typeof(CollectibleTreasure)); if (!prefab) { Debug.Log("Prefab is null."); } addToInventory(prefab); CollectibleTreasure temporaryThingIGrabbed = thingIGrabbed; thingIGrabbed = null; Destroy(temporaryThingIGrabbed.gameObject); } thingIGrabbed = null; }