/// <summary> /// Start picking up the item /// </summary> /// <param name="pickingItem"></param> /// <returns></returns> public override IEnumerator PickUpItem(GameObject pickingItem) { // Assign the item that is currently colliding with the armTip to the armTip's current holding item armTip.GetComponent <ArmUseItem>().currentlyHoldingItem = pickingItem; // Prevent the player from using the item right when they pick up that item armTip.GetComponent <ArmUseItem>().hasTriggerReleased = false; // Assign the start and stop using item function to the UnityEvents //UnityAction startUsingAction = System.Delegate.CreateDelegate(typeof(UnityAction), pickingItem.GetComponent<ItemInfo>(), "StartUsing") as UnityAction; //UnityEventTools.AddPersistentListener(armTip.GetComponent<ArmUseItem>().useItem, startUsingAction); //armTip.GetComponent<ArmUseItem>().useItem.AddListener(pickingItem.GetComponent<ItemInfo>().StartUsing); //UnityAction stopUsingAction = System.Delegate.CreateDelegate(typeof(UnityAction), pickingItem.GetComponent<ItemInfo>(), "StopUsing") as UnityAction; //UnityEventTools.AddPersistentListener(armTip.GetComponent<ArmUseItem>().stopUsingItem, stopUsingAction); //armTip.GetComponent<ArmUseItem>().stopUsingItem.AddListener(pickingItem.GetComponent<ItemInfo>().StopUsing); armTip.GetComponent <ArmUseItem>().useItemDelegate = System.Delegate.CreateDelegate(typeof(UseItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "StartUsing") as UseItemDelegateClass; armTip.GetComponent <ArmUseItem>().stopUsingItemDelegate = System.Delegate.CreateDelegate(typeof(StopUsingItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "StopUsing") as StopUsingItemDelegateClass; // If the other armTip is currently holding the item which is going to be holding by this armTip, then let the other arm drop the item first if (otherArm_Physics.armTip.GetComponent <ArmUseItem>().currentlyHoldingItem == pickingItem) { otherArm_Physics.DropDownItem(pickingItem); } yield return(new WaitForEndOfFrame()); //// If the item can be lifted by the arm, then disable it's gravity and raise it to the arm's height if (pickingItem.GetComponent <ItemInfo>().itemWeight <= armLiftingStrength) { // Change the item's velocity to 0 pickingItem.GetComponent <Rigidbody>().velocity = Vector3.zero; pickingItem.GetComponent <Rigidbody>().angularVelocity = Vector3.zero; // Raise the item to the arm's height pickingItem.transform.position = armTip.position; if (pickingItem.GetComponent <ItemInfo>().canUse) { //// Turn off the collider //TurnOffColliders(armTip.GetComponent<ArmUseItem>().currentlyHoldingItem); // Turn off mass, drag, etc. pickingItem.GetComponent <Rigidbody>().drag = 0; pickingItem.GetComponent <Rigidbody>().angularDrag = 0; pickingItem.GetComponent <Rigidbody>().mass = 0; //// Change the item to kinematic //pickingItem.GetComponent<Rigidbody>().isKinematic = true; // Rotate the item so the player can aim the usable item pickingItem.transform.rotation = armTip.rotation; } } //else //{ // Turn off the gravity of the picking item pickingItem.GetComponent <Rigidbody>().useGravity = false; // Add a fixed joint to the holding item to attach it to the armTip //pickingItem.gameObject.AddComponent<FixedJoint>(); //pickingItem.GetComponent<FixedJoint>().connectedBody = armTip.GetComponent<Rigidbody>(); //pickingItem.GetComponent<FixedJoint>().autoConfigureConnectedAnchor = true; //pickingItem.GetComponent<FixedJoint>().breakForce = armHoldingJointBreakForce; armTip.gameObject.AddComponent <FixedJoint>(); armTip.GetComponent <FixedJoint>().connectedBody = pickingItem.GetComponent <Rigidbody>(); //armTip.GetComponent<FixedJoint>().autoConfigureConnectedAnchor = true; armTip.GetComponent <FixedJoint>().breakForce = armHoldingJointBreakForce; //} pickingItem.GetComponent <ItemInfo>().isBeingHeld = true; pickingItem.GetComponent <ItemInfo>().holdingArm = armTip; }
/// <summary> /// Start picking up the item /// </summary> /// <param name="pickingItem"></param> /// <returns></returns> public IEnumerator PickUpItem(GameObject pickingItem) { // Assign the item that is currently colliding with the armTip to the armTip's current holding item armTip.GetComponent <ArmUseItem>().currentlyHoldingItem = pickingItem; // Prevent the player from using the item right when they pick up that item armTip.GetComponent <ArmUseItem>().hasTriggerReleased = false; // Assign the start and stop using item function to the UnityEvents //UnityAction startUsingAction = System.Delegate.CreateDelegate(typeof(UnityAction), pickingItem.GetComponent<ItemInfo>(), "StartUsing") as UnityAction; //UnityEventTools.AddPersistentListener(armTip.GetComponent<ArmUseItem>().useItem, startUsingAction); //armTip.GetComponent<ArmUseItem>().useItem.AddListener(pickingItem.GetComponent<ItemInfo>().StartUsing); //UnityAction stopUsingAction = System.Delegate.CreateDelegate(typeof(UnityAction), pickingItem.GetComponent<ItemInfo>(), "StopUsing") as UnityAction; //UnityEventTools.AddPersistentListener(armTip.GetComponent<ArmUseItem>().stopUsingItem, stopUsingAction); //armTip.GetComponent<ArmUseItem>().stopUsingItem.AddListener(pickingItem.GetComponent<ItemInfo>().StopUsing); armTip.GetComponent <ArmUseItem>().setupItemDelegate = System.Delegate.CreateDelegate(typeof(SetupItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "SetupItem") as SetupItemDelegateClass; armTip.GetComponent <ArmUseItem>().useItemDelegate = System.Delegate.CreateDelegate(typeof(UseItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "StartUsing") as UseItemDelegateClass; armTip.GetComponent <ArmUseItem>().stopUsingItemDelegate = System.Delegate.CreateDelegate(typeof(StopUsingItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "StopUsing") as StopUsingItemDelegateClass; armTip.GetComponent <ArmUseItem>().resetItemDelegate = System.Delegate.CreateDelegate(typeof(ResetItemDelegateClass), pickingItem.GetComponent <ItemInfo>(), "ResetItem") as ResetItemDelegateClass; // If the other armTip is currently holding the item which is going to be holding by this armTip, then let the other arm drop the item first if (otherArm_Physics.armTip.GetComponent <ArmUseItem>().currentlyHoldingItem == pickingItem) { otherArm_Physics.DropDownItem(pickingItem); } yield return(new WaitForEndOfFrame()); // Turn off the gravity of the picking item if (!pickingItem.GetComponent <ItemInfo>().fixedPosition) { pickingItem.GetComponent <Rigidbody>().useGravity = false; } //// If the item can be lifted by the arm, then disable it's gravity and raise it to the arm's height if (pickingItem.GetComponent <ItemInfo>().itemWeight <= armLiftingStrength && !pickingItem.GetComponent <ItemInfo>().fixedPosition) { // Change the item's velocity to 0 pickingItem.GetComponent <Rigidbody>().velocity = Vector3.zero; pickingItem.GetComponent <Rigidbody>().angularVelocity = Vector3.zero; // If the item can be moved if (!pickingItem.GetComponent <ItemInfo>().fixedPosition) { // Raise the item to the arm's height pickingItem.transform.position = armTip.position; } // Using spring joint armTip.gameObject.AddComponent <SpringJoint>(); armTip.GetComponent <SpringJoint>().connectedBody = pickingItem.GetComponent <Rigidbody>(); armTip.GetComponent <SpringJoint>().breakForce = armHoldingJointBreakForce; // Turn on drag for better spring physics pickingItem.GetComponent <Rigidbody>().drag = 30; pickingItem.GetComponent <Rigidbody>().angularDrag = 30; if (pickingItem.GetComponent <ItemInfo>().canUse) { //// Turn off the collider //TurnOffColliders(armTip.GetComponent<ArmUseItem>().currentlyHoldingItem); // Turn off mass pickingItem.GetComponent <Rigidbody>().mass = 0; //// Change the item to kinematic //pickingItem.GetComponent<Rigidbody>().isKinematic = true; // Rotate the item so the player can aim the usable item pickingItem.transform.rotation = armTip.rotation; // Setup spring joint armTip.GetComponent <SpringJoint>().spring = 0.0001f; armTip.GetComponent <SpringJoint>().damper = 0; } else { // Setup spring joint armTip.GetComponent <SpringJoint>().spring = pickingItem.GetComponent <Rigidbody>().mass * 1000f; armTip.GetComponent <SpringJoint>().damper = 0; } } else { // Using fixed joint armTip.gameObject.AddComponent <FixedJoint>(); armTip.GetComponent <FixedJoint>().connectedBody = pickingItem.GetComponent <Rigidbody>(); armTip.GetComponent <FixedJoint>().breakForce = armHoldingJointBreakForce; } // If the item cannot be moved if (pickingItem.GetComponent <ItemInfo>().fixedPosition&& pickingItem.GetComponent <ItemInfo>().canUse) { // Change the status indicator color pickingItem.GetComponent <ItemInfo>().ChangeIndicatorColor(pickingItem.GetComponent <ItemInfo>().isUsingStatusColor, 1); } // Add a fixed joint to the holding item to attach it to the armTip //pickingItem.gameObject.AddComponent<FixedJoint>(); //pickingItem.GetComponent<FixedJoint>().connectedBody = armTip.GetComponent<Rigidbody>(); //pickingItem.GetComponent<FixedJoint>().autoConfigureConnectedAnchor = true; //pickingItem.GetComponent<FixedJoint>().breakForce = armHoldingJointBreakForce; // Using fixed joint //armTip.gameObject.AddComponent<FixedJoint>(); //armTip.GetComponent<FixedJoint>().connectedBody = pickingItem.GetComponent<Rigidbody>(); //armTip.GetComponent<FixedJoint>().breakForce = armHoldingJointBreakForce; pickingItem.GetComponent <ItemInfo>().isBeingHeld = true; pickingItem.GetComponent <ItemInfo>().holdingArmTip = armTip; // If the ItemInfo component is enabled on the item to let it be setup when picked if (pickingItem.GetComponent <ItemInfo>().enabled) { armTip.GetComponent <ArmUseItem>().SetUpItem(); // Invoke the setup item event } }