//Rotates the spawn direction vector, then returns the spawn location for the next asteroid private Vector3 GetNextSpawnPos() { //Get the furthest possible spawn location in the current direction, then calcuate its distance from the center Vector3 MaxDistanceSpawnPos = SpawnDirection * SpawnDistanceRange.y; MaxDistanceSpawnPos = ScreenBounds.ClampPosInside(MaxDistanceSpawnPos); float MaxSpawnDistance = Vector3.Distance(Vector3.zero, MaxDistanceSpawnPos); //Get the new spawn pos somewhere along that line float SpawnDistance = Random.Range(SpawnDistanceRange.x, MaxSpawnDistance); Vector3 SpawnPos = SpawnDirection * SpawnDistance; //Rotate the spawn direction ready for next time, then return the final spawn position SpawnDirection = Quaternion.AngleAxis(-RotationPerSpawn, Vector3.forward) * SpawnDirection; return(SpawnPos); }
//Returns a position offset from the players current location, within the range of the given vector values private Vector3 GetOffsetPlayerPos(Vector2 OffsetRange) { //Get 2 random values within the given offset range to offset from the player pos in each direction float XOffset = Random.Range(OffsetRange.x, OffsetRange.y); float YOffset = Random.Range(OffsetRange.x, OffsetRange.y); //Flip a coin to decide which direction these offsets will be applied bool PositiveXOffset = Random.value >= 0.5f; bool PositiveYOffset = Random.value >= 0.5f; //Apply these offsets to the players current location to get the new offset position Vector3 OffsetPos = GameState.Instance.PlayerShip.transform.position; OffsetPos.x += PositiveXOffset ? XOffset : -XOffset; OffsetPos.y += PositiveYOffset ? YOffset : -YOffset; OffsetPos = ScreenBounds.ClampPosInside(OffsetPos); return(OffsetPos); }
private float RotationPerSpawn; //Spawn direction vector rotation after each spawn private void Start() { //Spawn all the stars into place RotationPerSpawn = 360f / StarAmount; for (int i = 0; i < StarAmount; i++) { //Get the furthest distance possible spawn location in the current spawn direction, then calculate its distance Vector3 MaxDistanceSpawnPos = SpawnDirection * MaxSpawnDistance; MaxDistanceSpawnPos = ScreenBounds.ClampPosInside(MaxDistanceSpawnPos); float MaxValidSpawnDistance = Vector3.Distance(Vector3.zero, MaxDistanceSpawnPos); //Spawn a new star in a random distance between 0 and this max distance float RandomSpawnDistance = Random.Range(0f, MaxValidSpawnDistance); Vector3 SpawnPos = SpawnDirection * RandomSpawnDistance; Instantiate(StarPrefab, SpawnPos, Quaternion.identity); //Rotate the spawn direction vector, ready for the next star to be spawned SpawnDirection = Quaternion.AngleAxis(-RotationPerSpawn, Vector3.forward) * SpawnDirection; } }