/* Define how this object interacts with its surroundings */ protected IEnumerator Interact() { Ray ray; RaycastHit hit; while (true) { switch (currState) { case (int)States.REST: // Check if the object can be picked up // Allow interaction if possible ray = new Ray(targets[PLAYER].transform.position, targets[PLAYER].transform.forward); if (Physics.Raycast(ray, out hit, pickUpRange, layerMask: 1 << pickUpLayer)) { AnimatePickUp(); } break; case (int)States.PICKUP: // Check if the object can be thrown at something (in range and has a SmartTarget script) // Allow interaction if possible ray = new Ray(targets[PLAYER].transform.position, targets[PLAYER].transform.forward); if (Physics.Raycast(ray, out hit, throwRange, layerMask: 1 << throwLayer)) { SmartTarget st = hit.transform.gameObject.GetComponent <SmartTarget>(); if (st != null && st.CanHit(self)) { // Indicate that the object can be hit // Throw the object AnimateThrow(hit); } } break; } yield return(null); } }