// Adds new asteroids to the scene for the wave private void SpawnAsteroids() { DestroyExistingAsteroids(); // remove old asteroids, but there shouldn't be any. numAsteroids = 4 + wave; // asteroid number increases with each wave for (int i = 0; i < numAsteroids; i++) { GameObject newAsteroidTemp = Instantiate(asteroidLarge); newAsteroidTemp.GetComponent <AsteroidController>().SetHasBeenInside(false); // this makes sure the code knows that it is coming from outside the screen area BorderShift bs = newAsteroidTemp.GetComponent <BorderShift>(); bs.ForceInit(); // I don't like doing this, but when the game starts the border shift hasn't derived screen space info yet float minDist = bs.GetDiagonalDistance(); // distance from centre of screen to the corner Vector2 startPosition = GetAsteroidStartPosition(i, Random.Range(minDist + 2, minDist + 5)); // distributed start position with randomised distance newAsteroidTemp.transform.position = new Vector3(startPosition.x, startPosition.y, 0); float rotationTempDegrees = GetAsteroidStartRotation(startPosition, new Vector2(Random.Range(-3, 3), Random.Range(-3, 3))); newAsteroidTemp.transform.Rotate(0, 0, rotationTempDegrees); // rotate the asteroid so it points towards the target newAsteroidTemp.GetComponent <AsteroidController>().InitMovement(); // this randomised velocity and angular momentum of the asteroid newAsteroidTemp.transform.SetParent(asteroidHolder.transform); // add to the asteroid holder gameobject } }
void Start() { // grab the relevant components borderShift = GetComponent <BorderShift>(); rb = GetComponent <Rigidbody2D>(); pc = GetComponent <PolygonCollider2D>(); GameObject gameControllerObject = GameObject.FindWithTag("gameController"); gameController = gameControllerObject.GetComponent <GameController>(); }