void SpawnHopTarget() { curHopTarget = CacheManager.ActivateRandom("Basic_HopTarget").GetComponent <HopTarget>(); Vector2 hopTargetPos; if (hopperState == HOPPERSTATES.IDLE) // if hopper is not moving/idle, spawn same X with player { hopTargetPos = new Vector2(player.transform.position.x, Random.Range(Constants.MIN_SPAWNABLE_Y, Constants.MAX_SPAWNABLE_Y)); } else { hopTargetPos = new Vector2(Random.Range(Constants.MIN_SPAWNABLE_X, Constants.MAX_SPAWNABLE_X), Random.Range(Constants.MIN_SPAWNABLE_Y, Constants.MAX_SPAWNABLE_Y)); } //spawn initially off screen then animate to supposed position curHopTarget.transform.position = new Vector2(hopTargetPos.x, Constants.OFFSCREEN_POINT_Y); StartCoroutine(PositionHopTarget(hopTargetPos)); int colorIndex = Random.Range(0, Constants.NUMBER_OF_HOPCOLORS); curHopTarget.CurHopColor = (HopColor)colorIndex; curHopTarget.sprite.color = SetColorAppearance(curHopTarget.CurHopColor); }
// animate hopping IEnumerator Hop() { float targetYPos = curHopTarget.transform.position.y; float t_percent = 0; float distanceToHop = targetYPos - player.transform.position.y; //initial distance to be travelled by hopper to reach current hop target bool hasHit = false; //while player not reached same y pos with target, move closer while (System.Math.Round(targetYPos, 1) > System.Math.Round(player.transform.position.y, 1)) { player.transform.Translate(Vector2.up * 25 * Time.deltaTime); //animate scaling t_percent = (player.transform.position.y - restPos.y) / distanceToHop; //percent of distance travelled in decimal player.transform.localScale = Vector2.one * hopperScaleAnimationCurve.Evaluate(t_percent); Debug.Log(targetYPos + " " + player.transform.position.y); //check collisions while approaching if (curHopTarget.GetComponent <Collider2D>() == Physics2D.OverlapCircle(player.transform.position, playerRadius) && hopperColor == curHopTarget.CurHopColor) { //successful hit curHopTarget.Explode(); Camera.main.GetComponent <CameraShake>().Shake(.1f, .02f); curHopTarget = null; StartCoroutine(ReturnToRestPos()); gameScore++; UpdateScore(gameScore); hasHit = true; player.transform.localScale = Vector2.one * hopperScaleAnimationCurve.Evaluate(1); break; } yield return(null); } // this is the moment when player reached the target //put collision conditions here if (!hasHit) { Debug.Log("CHECKING COLLISION"); if (curHopTarget.GetComponent <Collider2D>() == Physics2D.OverlapCircle(player.transform.position, playerRadius) && hopperColor == curHopTarget.CurHopColor) { //successful hit curHopTarget.Explode(); Camera.main.GetComponent <CameraShake>().Shake(.1f, .02f); curHopTarget = null; StartCoroutine(ReturnToRestPos()); gameScore++; UpdateScore(gameScore); } else { //missed curHopTarget.Explode(); curHopTarget = null; EventsManager.OnGameOver.Invoke(); } } Debug.Log("Done hopping!"); }