void Update() { // Get the bounds of the cube var bounds = boxCollider.bounds.extents; var topFrontLeftPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x - bounds.x, transform.position.y - bounds.y, transform.position.z + bounds.z)); var topRearLeftPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x - bounds.x, transform.position.y - bounds.y, transform.position.z - bounds.z)); var topFrontRightPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x + bounds.x, transform.position.y - bounds.y, transform.position.z + bounds.z)); var topRearRightPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x + bounds.x, transform.position.y - bounds.y, transform.position.z - bounds.z)); var bottomFrontLeftPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x - bounds.x, transform.position.y + bounds.y, transform.position.z + bounds.z)); var bottomRearLeftPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x - bounds.x, transform.position.y + bounds.y, transform.position.z - bounds.z)); var bottomFrontRightPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x + bounds.x, transform.position.y + bounds.y, transform.position.z + bounds.z)); var bottomRearRightPoint = Camera.main.WorldToViewportPoint(new Vector3(transform.position.x + bounds.x, transform.position.y + bounds.y, transform.position.z - bounds.z)); // Check if any of the points are inside the camera view if (Camera.main.rect.Contains(topFrontLeftPoint) == false && Camera.main.rect.Contains(topRearLeftPoint) == false && Camera.main.rect.Contains(bottomFrontLeftPoint) == false && Camera.main.rect.Contains(bottomRearLeftPoint) == false && Camera.main.rect.Contains(topFrontRightPoint) == false && Camera.main.rect.Contains(topRearRightPoint) == false && Camera.main.rect.Contains(bottomFrontRightPoint) == false && Camera.main.rect.Contains(bottomRearRightPoint) == false) { // Reset to the initial state line.ResetLine(); line.drawLine = false; rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; transform.rotation = Quaternion.identity; transform.position = initialPosition; cube.HandleInputEnd(Input.mousePosition); } }
void Update() { if (Input.GetMouseButtonDown(1)) { if (Input.GetMouseButton(0)) { drawLine = true; } } if (Input.GetMouseButtonUp(0)) { drawLine = false; } if (Input.GetMouseButtonUp(1)) { drawLine = false; if (isAlive) { ResetLine(); } } if (drawLine && isAlive) { var position = transform.position; if (pointsList.Count == 0) { pointsList.Add(position); line.positionCount = pointsList.Count; line.SetPosition(pointsList.Count - 1, pointsList [pointsList.Count - 1]); } // Check minimum mouse distance from last point before adding a new point else if (Vector3.Distance(pointsList [pointsList.Count - 1], position) > 0.01f) { pointsList.Add(position); score = GetLineLenght(); // Check if collides if (Collides()) { if (score > highScore) { highScore = score; } isAlive = false; drawLine = false; line.startColor = Color.red; line.endColor = Color.red; cube.HandleInputEnd(Input.mousePosition); } line.positionCount = pointsList.Count; line.SetPosition(pointsList.Count - 1, pointsList [pointsList.Count - 1]); // Update score and highscore text scoreText.text = "SCORE " + System.Math.Round(score, 2) + "\n" + "HIGHSCORE " + System.Math.Round(highScore, 2); PlayerPrefs.SetFloat("highscore", (float)highScore); } } }