public IEnumerator WaitForPlayerToStop(float poleDistance, float poleHeight, string currentPoleColl, string currentPoleMagnetism, string currentPoleFriction, string currentPoleMagnetismArea) { string currentPoleName = currentPoleColl; string currentPoleMag = currentPoleMagnetism; string currentPoleFric = currentPoleFriction; string currentPoleMagAr = currentPoleMagnetismArea; // Setting a weird algorithm to check whether ray still can be spotted or not ! // Here, we are setting a short amount of delay when the player has certain level of velocity float playersXvelocity = GameObject.FindGameObjectWithTag("Player").GetComponent <Rigidbody2D>().velocity.x; if (playersXvelocity > 3) { yield return(new WaitForSeconds(0.75F)); } if (playersXvelocity <= 3 && playersXvelocity > 2) { yield return(new WaitForSeconds(0.50F)); } //yield return new WaitUntil(playersXvelocity == 0); else if (playersXvelocity <= 2 && playersXvelocity > 1) { yield return(new WaitForSeconds(0.5F)); } else if (playersXvelocity <= 1 && playersXvelocity > 0) { yield return(new WaitForSeconds(0.25F)); } else { yield return(new WaitForSeconds(0.10F)); } GameObject loggerObject = GameObject.Find("Logger"); LoggerScript logger = loggerObject.GetComponent <LoggerScript>(); var ray = GetComponent <rayCastLogic>(); if (ray.isSpotted()) { ScoreAdded(currentPoleName); // Write in to Files - although the file writing procedure is on database right now logger.loggerMethod(1, poleDistance, poleHeight, currentPoleName, currentPoleMag, currentPoleFric, score, currentPoleMagAr, nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel); //Debug.Log(currentPoleName); // Find the Successive Pole Name here GameObject whatPole = GameObject.Find(currentPoleName); successivePoleX = whatPole.transform.position.x; successivePoleY = whatPole.transform.position.y; successivePoleName = currentPoleName; } else { logger.loggerMethod(0, poleDistance, poleHeight, currentPoleName, currentPoleMag, currentPoleFric, score, currentPoleMagAr, nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel); } }
void OnCollisionEnter2D(Collision2D collider) { GameObject loggerObject = GameObject.Find("Logger"); LoggerScript logger = loggerObject.GetComponent <LoggerScript>(); float poleDistance_fl = 0; float poleHeight = 0; string currentPoleMagnetism = null; string currentPoleFriction = null; string currentPoleMagnetismArea = null; if (collider.gameObject.tag == "Pole" && collider.gameObject.name != "PoleEnd") { // Play the animation for the player animator.SetBool("doJump", false); animator.SetBool("doIdle", true); animator.SetBool("doRun", false); // Get the current Pole name with which the pole get collided currentPoleColl = collider.gameObject.name; // Name of the pole the player just got hit // if the player is on the first pole if (currentPoleColl == "Pole00") { currentPoleCollX = collider.gameObject.transform.position.x; //Debug.Log("For Pole 01: " + currentPoleCollX); prevPoleCollX = currentPoleCollX; // after player hits the first pole we set the previous pole to Pole00 poleDistance_fl = 0; GameOverCanvasReset = true; // Resetting GameOverCanvas back to false //Debug.Log("Is Game Over Canvas Reset: " + GameOverCanvasReset); successivePoleX = currentPoleCollX; successivePoleY = collider.gameObject.transform.position.y; } // but if the collder is other than the first pole then else { currentPoleCollX = collider.gameObject.transform.position.x; // current pole X is the current collider's X position poleDistance_fl = currentPoleCollX - prevPoleCollX; // pole distance calculation prevPoleCollX = currentPoleCollX; // setting the current pole to prev pole again } poleHeight = collider.gameObject.transform.position.y; currentPoleMagnetism = collider.gameObject.GetComponentInChildren <PointEffector2D>().forceMagnitude.ToString(); currentPoleFriction = collider.gameObject.GetComponent <BoxCollider2D>().sharedMaterial.friction.ToString(); currentPoleMagnetismArea = collider.gameObject.GetComponentInChildren <CircleCollider2D>().radius.ToString(); if (currentPoleMagnetismArea == "6") { currentPoleMagnetismArea = "H"; } else if (currentPoleMagnetismArea == "3") { currentPoleMagnetismArea = "L"; } else { currentPoleMagnetismArea = "M"; } // Call the raycasting here var ray = GetComponent <rayCastLogic>(); if (ray.isSpotted()) { // Start a coroutine to set up small wait until the player stops StartCoroutine(WaitForPlayerToStop(poleDistance_fl, poleHeight, currentPoleColl, currentPoleMagnetism, currentPoleFriction, currentPoleMagnetismArea)); } } else if (collider.gameObject.tag == "Ground") { //print("Collsion occured with: " + collider.gameObject.tag + "Player has fall down"); //animator.SetBool("doJump", false); //animator.SetBool("doIdle", false); animator.SetTrigger("doDeath"); gameSounds.PlayOneShot(audioFall, 0.5F); MainScript.playerIsAlive = false; // Log in to files logger.loggerMethod(0, 0, 0, "null", "null", "null", score, "null", nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel); // Checking if the ninja hits the ground; in this case restart the game //if (!audioFall.isPlaying) audioFall.Play(); if (GameOverCanvasReset == true) { GameOverCanvasReset = false; //Debug.Log("Is Game Over Canvas Reset: " + GameOverCanvasReset); StartCoroutine(GameEnd()); // shortly wait for the unity engine to reload the scene } } else if (collider.gameObject.name == "PoleEnd") { var ray = GetComponent <rayCastLogic>(); if (ray.isSpotted()) { // Update Score ScoreAdded(collider.gameObject.name); // Set player to !Alive to stop further mouse button click MainScript.playerIsAlive = false; // Start running animator.SetBool("doRun", true); // Log logger.loggerMethod(1, poleDistance_fl, poleHeight, "PoleEnd", "0", "0", score, "null", "poleEnd", distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel); StartCoroutine(GameComplete()); } } if (collider.gameObject.name != "PoleEnd") { // Set the pole's magnetism false when player collides with it poleMagObjects = GameObject.FindGameObjectsWithTag("PoleMagnetism"); foreach (GameObject pole in poleMagObjects) { if (pole.GetComponent <PointEffector2D>().enabled) { pole.GetComponent <PointEffector2D>().enabled = false; } } } }