void CastRay() { // Create ray cast from mouse input Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Plane playerPlane = new Plane(Vector3.back, player.transform.position); // Set player float distance; // Creates a ray cast from player to mouse click if (playerPlane.Raycast(ray, out distance)) { Vector3 hitPt = ray.GetPoint(distance); Ray playerToClick = new Ray(player.transform.position, hitPt - player.transform.position); //RaycastHit2D hit = Physics2D.Raycast(playerToClick.origin, playerToClick.direction, pickUpRange, LayerMask); RaycastHit2D hit = Physics2D.Raycast(playerToClick.origin, playerToClick.direction, float.MaxValue, LayerMask); // Debug.DrawRay(playerToClick.origin, playerToClick.direction * 50, Color.yellow); if (hit) { Debug.Log(hit.collider.gameObject.name); //int collisionLayerMask = 1 << hit.collider.gameObject.layer; TurtleController turtleController = hit.collider.gameObject.GetComponent <TurtleController>(); FallingPlasticController fallingPlasticController = hit.collider.gameObject.GetComponent <FallingPlasticController>(); if (currentCoroutine != null) { StopCoroutine(currentCoroutine); } currentCoroutine = StartCoroutine(CheckIfNear(turtleController, fallingPlasticController)); } } }
public void OnChildCollision(TurtleChildCollision childPart, Collider2D collider) { int collisionLayerMask = 1 << collider.gameObject.layer; // Check if already choking if (!isChoking) { // If collides with PlasticData if (collisionLayerMask == plasticLayerMask.value) { plastic = collider; // If collides with Mouth if (childPart == mouth) { isChoking = true; curState = FSMState.Choke; plasticRB = plastic.GetComponent <Rigidbody2D>(); plasticRB.constraints = RigidbodyConstraints2D.FreezePosition; // Show an outline to easily see on which plastic is the turtle choking plasticInContact = plastic.GetComponent <FallingPlasticController>(); plasticInContact.ShowOutline(false); } // If collides with Sight if (childPart == sight) { curState = FSMState.Chase; } } } }
IEnumerator PickupPlastic(FallingPlasticController fallingPlasticController) { collectedPlastic = fallingPlasticController.gameObject; // Show an outline to the plastic being picked up fallingPlasticController.ShowOutline(); yield return(new WaitForSeconds(rescuingAnimation.length)); animator.SetBool("isRescuing", false); enabled = true; GetComponent <Movement>().enabled = true; fallingPlasticController.onPickup.Raise(); fallingPlasticController.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.None; fallingPlasticController.gameObject.SetActive(false); }
IEnumerator CheckIfNear(TurtleController tc, FallingPlasticController pc) { if (tc) { yield return(new WaitUntil(() => Vector2.Distance(player.transform.position, tc.transform.position) <= pickUpRange)); if (tc.isChoking && tc.isAlive) { animator.SetBool("isRescuing", true); tc.plasticInContact.ShowOutline(true); // Show the outline similar to pickup StartCoroutine(Rescue(tc)); } } else if (pc) { yield return(new WaitUntil(() => Vector2.Distance(player.transform.position, pc.transform.position) <= pickUpRange)); animator.SetBool("isRescuing", true); rb.velocity = Vector3.zero; pc.PickUp(); StartCoroutine(PickupPlastic(pc)); } }
IEnumerator SpawnRandomObjects() { yield return(new WaitForSeconds(Random.Range(MinimumSpawnRate, MaximumSpawnRate))); int i = Random.Range(0, PooledObjectList.Count); // Spawn a random prefab from pooled object list spawnLocationIndex = Random.Range(0, SpawnLocations.Length); // Checks if pooled object is not active if (!PooledObjectList[i].activeInHierarchy) { PooledObjectList[i].transform.position = SpawnLocations[spawnLocationIndex].position; // Sets spawn location back to originanl position PooledObjectList[i].SetActive(true); // Activate the pooled object // Hack: If this object is a plastic, call its ResetObject method FallingPlasticController plasticController = PooledObjectList[i].GetComponent <FallingPlasticController>(); if (plasticController) { plasticController.ResetObject(); } } StartCoroutine(SpawnRandomObjects()); // Restart the process }
private void ChokeState() { //Display the ChokeUI chokeUI.gameObject.SetActive(true); if (isChoking && plastic.gameObject.activeSelf) { StartCoroutine(Choke()); animator.SetBool("isChoking", true); //OnChoke.Raise(); } else { StopAllCoroutines(); // Stop choking currentChokingDuration = chokeDuration; //Reset current choke duration chokeUI.gameObject.SetActive(false); // Hide choke ui animator.SetBool("isChoking", false); isChoking = false; plastic.gameObject.SetActive(false); // Remove plastic plasticInContact.ResetObject(); // Reset plastic's outline plasticInContact = null; // Remove the reference curState = FSMState.Wander; // Wander } }