// Update is called once per frame void Update() { for (int i = 0; i < balls.Count; i++) { GameObject b1 = balls[i]; Ball2D ball = b1.GetComponent <Ball2D>(); ball.UpdatePhysics(); for (int j = 0; j < balls.Count; j++) { GameObject b2 = balls[j]; Ball2D ball2 = b2.GetComponent <Ball2D>(); if (ball != ball2) { if (ball.isCollidingWith(ball2)) { handleBallColli(ball, ball2, Time.deltaTime); } } } for (int k = 0; k < holes.Count; k++) { Hole2D hole = holes[k].GetComponent <Hole2D>(); if (ball.isInside(hole)) { b1.SetActive(false); break; } } } }
// Falls inside a hole public bool isInside(Hole2D hole) { HVector2D thisBall = new HVector2D(transform.position.x, transform.position.y); HVector2D holePos = new HVector2D(hole.transform.position.x, hole.transform.position.y); return(GlobalVariable.findDistance(thisBall, holePos) <= hole.mRadius); }
// Falls inside a hole public bool isInside(Hole2D hole) { //finds diff betw the x and y values float differenceInX = this.mPos.x - hole.mPos.x; float differenceInY = this.mPos.y - hole.mPos.y; //finds the distance/magnitude between ball and hole float distanceBetBallAndHole = Mathf.Sqrt(Mathf.Pow(differenceInX, 2) + Mathf.Pow(differenceInY, 2)); if (distanceBetBallAndHole <= hole.mRadius) { return(true); } return(false); }
// Update is called once per frame void Update() { for (int i = 0; i < balls.Count; i++) { GameObject b1 = balls[i]; Ball2D ball = b1.GetComponent <Ball2D>(); ball.UpdatePhysics(); for (int j = 0; j < balls.Count; j++) { GameObject b2 = balls[j]; Ball2D ball2 = b2.GetComponent <Ball2D>(); if (ball != ball2) { if (ball.isCollidingWith(ball2)) { handleBallColli(ball, ball2, Time.deltaTime); AudioSource.PlayClipAtPoint(hitSound, transform.position); } } } for (int k = 0; k < holes.Count; k++) { Hole2D hole = holes[k].GetComponent <Hole2D>(); if (ball.isInside(hole)) { b1.SetActive(false); if (b1.gameObject.tag == "Player") { gameOver.SetActive(true); Time.timeScale = .25f; Invoke("Reset", 1.0f); } break; } } } }