Exemplo n.º 1
0
    private void PlaceBall()
    {
        Vector3 spawnPoint = new Vector3(), spawnVelocity = new Vector3();

        for (int attempt = 0; attempt < 25; attempt++)
        {
            spawnPoint    = new Vector3(Random.value * 16 - 8, Random.value * meanBallHeight * 2.0f, Random.value * 16 - 8);
            spawnVelocity = Random.insideUnitSphere * meanBallVelocity * 4.0f / 3.0f;

            // Ensure the ball has enough total energy to warrant observation; could work backwards from this limitation instead, not a priority
            if ((spawnVelocity.sqrMagnitude / 2 - (spawnPoint.y - ballPrefab.radius) * Physics.gravity.y) * ballPrefab.mass > minBallEnergy)
            {
                break;
            }
        }

        if (ball != null)
        {
            ball.gameObject.SetActive(false);
            Destroy(ball.gameObject);
        }
        ball = Instantiate(ballPrefab, spawnPoint, Quaternion.identity);
        ball.Initialize(spawnVelocity);
    }