public void PlayGrabFailAnimation(GrabFailReason reason) { string animation = ""; if (reason == GrabFailReason.Big) { animation = "big"; } else if (reason == GrabFailReason.Small) { animation = "small"; } Reticle.GetComponent <Animator>().SetTrigger(animation); }
/// <summary> /// Handle selecting and picking up items. /// </summary> private void HandlePickingUpItems() { if (m_heldItem != null) { return; } // Look at an item. HeldItem selectedItem = null; GrabFailReason reason = GrabFailReason.Range; RaycastHit hitInfo; if (Physics.Raycast(PlayerCamera.transform.position, PlayerCamera.transform.forward, out hitInfo, PickupItemDistance * Scale, LayerMask.GetMask("HeldItem"))) { HeldItem lookedAtItem = hitInfo.transform.GetComponent <HeldItem>(); if (lookedAtItem == null) { lookedAtItem = hitInfo.transform.GetComponentInParent <HeldItem>(); } if (CanSelectItemBasedOnScale(lookedAtItem, out reason)) { selectedItem = lookedAtItem; lookedAtItem.SetSelectionVisual(true); } else { lookedAtItem.SetSelectionVisual(false); } } // If a valid item is looked at, you can pick it up with a button. if (Input.GetButtonDown("Grab") && !m_grabbedThisFrame) { m_grabbedThisFrame = true; if (selectedItem == null) { GameManager.Instance.GameCanvas.PlayGrabFailAnimation(reason); } else { PickUpItem(selectedItem); } } }
/// <summary> /// Handle selecting buttons. /// </summary> private void HandlePressingButtons() { // Look at a button RaycastHit hitInfo; if (Physics.Raycast(PlayerCamera.transform.position, PlayerCamera.transform.forward, out hitInfo, PickupItemDistance * Scale, LayerMask.GetMask("GrowButton", "HeldItem"))) { GrowButton lookedAtItem = hitInfo.transform.GetComponent <GrowButton>(); if (lookedAtItem != null) { GrabFailReason reason = GrabFailReason.Range; if (CanSelectItemBasedOnScale(lookedAtItem, out reason)) { // If a valid item is looked at, you can pick it up with a button. if (Input.GetButtonDown("Grab") && !m_grabbedThisFrame) { m_grabbedThisFrame = true; PressButton(lookedAtItem); } lookedAtItem.SetSelectionVisual(true); } else { // If an invalid item is looked at, do the animation if (Input.GetButtonDown("Grab") && !m_grabbedThisFrame) { m_grabbedThisFrame = true; GameManager.Instance.GameCanvas.PlayGrabFailAnimation(reason); } lookedAtItem.SetSelectionVisual(false); } } else if (lookedAtItem != null) { } } }
/// <summary> /// Can I pick it up. /// </summary> private bool CanSelectItemBasedOnScale(SelectableItem item, out GrabFailReason reason) { float itemBounds = item.GetMaxBounds(); Vector3 boundVector = Visual.GetComponent <Renderer>().bounds.size *Scale; float selfBounds = Mathf.Max(Mathf.Max(boundVector.x, boundVector.y), boundVector.z); float boundsRatio = itemBounds / selfBounds; Debug.Log(string.Format("Bounds Ratio: {0} Self Bounds: {1} Item Bounds: {2}", boundsRatio, selfBounds, itemBounds)); reason = GrabFailReason.Range; if (boundsRatio < MinHeldItemBoundsRatio) { reason = GrabFailReason.Big; } else if (boundsRatio > MaxHeldItemBoundsRatio) { reason = GrabFailReason.Small; } return(boundsRatio <= MaxHeldItemBoundsRatio && boundsRatio >= MinHeldItemBoundsRatio); }