Skip to content

Instantly share code, notes, and snippets.

@Volorf
Created August 14, 2024 16:03
Show Gist options
  • Save Volorf/035a224fe1a1099544ef5ce43dad43e2 to your computer and use it in GitHub Desktop.
Save Volorf/035a224fe1a1099544ef5ce43dad43e2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class PlanetSpawner : MonoBehaviour
{
[SerializeField] List<GameObject> planets;
[SerializeField] Vector3 spawnVolume;
[SerializeField] int numberOfPlanets = 100;
[SerializeField] GameObject spaceShip;
void Start()
{
GeneratePlanets();
}
void GeneratePlanets()
{
print("hey");
for(int i = 0; i < numberOfPlanets; i++)
{
GameObject go = Instantiate(GetRandomPlanet(), GetRandomPosition(), Quaternion.identity);
go.transform.parent = transform;
}
}
void Update()
{
float distance = Vector3.Distance(spaceShip.transform.position, transform.position);
if (distance > 100)
{
transform.position = spaceShip.transform.position;
}
}
Vector3 GetRandomPosition()
{
return new Vector3(Random.Range(-spawnVolume.x, spawnVolume.x),
Random.Range(-spawnVolume.y, spawnVolume.y),
Random.Range(-spawnVolume.z, spawnVolume.x));
}
public GameObject GetRandomPlanet()
{
return planets[Random.Range(0, planets.Count)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment