void OnCollisionEnter2D(Collision2D other) { //fishState = setFishState(FishMode.swim); //direction = -direction; if (!other.gameObject.CompareTag("pickup")) //check // other.gameObject.CompareTag ("fishBoundary") { startPosition = rb2d.position; //print ("collision"); if (collisionTimer > 4f) { //print ("Stuck in corner"); fishState = setFishState(FishMode.stuck); } collisionTimer = 5f; } foreach (ContactPoint2D contact in other.contacts) { //collisionPosition2 = collisionPosition1; collisionPosition1 = contact.point; collisionNormal2 = collisionNormal1; collisionNormal1 = contact.normal; if (fishState != FishMode.escape && fishState != FishMode.escape2 && fishState != FishMode.stuck) //check { direction = collisionNormal1; print("Fish is not stuck, or escaping"); } Debug.DrawRay(contact.point, contact.normal, Color.magenta, 2f); // Do something with normal here } }
// This function handles the transitions to make sure something only happens once. public FishMode setFishState(FishMode newState) { switch (newState) { case FishMode.swim: startPosition = rb2d.position; direction = RandomVector2(); break; case FishMode.swimDown: startPosition = rb2d.position; direction = DownVector2(); break; case FishMode.escape: startPosition = rb2d.position; //rb2d.velocity = Vector2.zero; direction = EscapeVector2(); //rb2d.AddForce (direction * escapeForce); break; case FishMode.escape2: // this is called when a fish has collided in the last 5 sends and puffin is pursuing startPosition = rb2d.position; //rb2d.velocity = Vector2.zero; direction = EscapeVector2AndWall(); break; case FishMode.stuck: startPosition = rb2d.position; direction = CornerEscape(); break; case FishMode.still: //rb2d.velocity = Vector2.zero; break; case FishMode.caught: break; default: break; } //print ("The Fish's state is now " + newState + " Collision timer is " + collisionTimer); return(newState); }
// Update is called once per frame void Update() { //Flip (); /.this is now handled in rotate Rotate(); // check to see if puffin is close enough to Open his mouth, if so set bool true if (!inPuffinRange && puffinToFish.sqrMagnitude < GameManager.instance.puffinToMouthRange * GameManager.instance.puffinToMouthRange) { // if the fish is in the range of the puffin, add one to the count // to make mouth open GameManager.instance.fishCloseToPuffin = GameManager.instance.fishCloseToPuffin + 1; //print (GameManager.instance.fishCloseToPuffin + " Fish in Range" + puffinToFish.sqrMagnitude + " is the closeness. Puffin to Fish is " + puffinToFish + " It is less than Closeness criteria " + GameManager.instance.puffinToMouthRange * GameManager.instance.puffinToMouthRange); inPuffinRange = true; } if (inPuffinRange && puffinToFish.sqrMagnitude > GameManager.instance.puffinToMouthRange * GameManager.instance.puffinToMouthRange) { // if the fish moves out ofthe range of the puffin, subtract one from the count if count is greater than 0// to make mouth close GameManager.instance.fishCloseToPuffin = GameManager.instance.fishCloseToPuffin > 0 ? GameManager.instance.fishCloseToPuffin - 1: GameManager.instance.fishCloseToPuffin; //GameManager.instance.fishCloseToPuffin = GameManager.instance.fishCloseToPuffin - 1; inPuffinRange = false; } //countdown timer in seconds. Greater than 0, counts down to o. collisionTimer = collisionTimer > 0 ? collisionTimer - Time.deltaTime : 0f; //this countdowns from fish last hit something if ((rb2d.position - collisionPosition1).sqrMagnitude > escapeZoneMax * escapeZoneMax) // checks if the fish is farther than the escape xone and if so, reset timer { collisionTimer = 0f; } if (puffin != null) // this is so there isn't issues when the puffin is destroyed sometimes { puffinToFish = (transform.position - puffin.transform.position); } else { puffinToFish = Vector2.one * Mathf.Infinity; } //distance from fish to puffin puffinApproachAngle = Mathf.Atan2(puffinToFish.y, puffinToFish.x) * Mathf.Rad2Deg; //if(puffinApproachAngle > 180 //Vector2.Angle(puffin.transform.position, transform.position);; //the angle between puffin and fish //puffintoFishRelativeVelocityDir = Vector2.Angle (puffinrb2d.velocity, rb2d.velocity);// difference in direction from puffin to fish velocity if (fishInWater && !fishInAir && fishState != FishMode.swim && fishState != FishMode.swimDown && puffinToFish.sqrMagnitude > flightZoneMax * flightZoneMax) { fishState = setFishState(FishMode.swim); } if (fishInAir && !fishInWater && fishState != FishMode.still) { fishState = setFishState(FishMode.still); } if (fishInAir && fishInWater && fishState != FishMode.swimDown) { fishState = setFishState(FishMode.swimDown); //fishState = setFishState(FishMode.escape2); } //print (puffinToFish.magnitude); if (puffinToFish.sqrMagnitude < flightZoneMax * flightZoneMax && fishState != FishMode.escape && fishInWater && fishState != FishMode.escape2 && fishState != FishMode.stuck) { if (collisionTimer != 0f) //&& (puffinApproachAngle > 15f || puffinApproachAngle < -15f) { fishState = setFishState(FishMode.escape2); } else { fishState = setFishState(FishMode.escape); } //print ("Set Escape"); //print (puffinApproachAngle); } switch (fishState) { case FishMode.swim: rb2d.velocity = direction * swimSpeed; if ((rb2d.position - startPosition).sqrMagnitude > 20 * 20) { //Move in new random direction direction = RandomVector2(); startPosition = rb2d.position; } break; case FishMode.swimDown: rb2d.velocity = direction * swimSpeed; if ((rb2d.position - startPosition).sqrMagnitude > 10 * 10) { fishState = setFishState(FishMode.swim); } break; case FishMode.escape: rb2d.velocity = direction * escapeSpeed; //how to script if the puffin's approach changes too much, to change directions //Vector2. if (collisionTimer != 0f) { setFishState(FishMode.escape2); } else if (puffinApproachAngle > 30f || puffinApproachAngle < -30f) //change direction based on fish angle { //print (puffinApproachAngle); direction = EscapeVector2(); } //only changes if 5 seconds passed { //print (puffinApproachAngle); } //rb2d.AddForce (puffinToFish.normalized * escapeForce); break; case FishMode.escape2: rb2d.velocity = direction * escapeSpeed; //*1.5f; if (puffinApproachAngle > 30f || puffinApproachAngle < -30f) { direction = EscapeVector2AndWall(); } break; case FishMode.stuck: rb2d.velocity = direction * escapeSpeed; // * 1.5f; if (puffinApproachAngle > 45f || puffinApproachAngle < -45f) { direction = CornerEscape(); } if ((rb2d.position - startPosition).sqrMagnitude > 10 * 10) { setFishState(FishMode.escape); } break; case FishMode.still: break; case FishMode.caught: break; default: break; } }