Hello everyone, first time here, and mainly just experimenting and messing around in Unity to pass some time, I’m honestly having a blast so far. Let me preface this by saying im a beginner and by no means have a single clue what I got myself into lol. I’ve been using ChatGPT (talking AI) for those that don’t know it can help out with creating code, granted sometimes it works, and other times it breaks and without it I wouldn’t have gotten as far as I did.
So my question is, I’m trying to generate Stars (which i created with this amazing tutorial here:
) based on the camera being the generator. The AI recommended that i use Chunks to load more stars once the camera enters said chunk and i can tinker with those values (adding a screen shot for that:
So the issue I ran into and can’t seem to figure out even with the help of that AI is that once I leave the starting area the stars that generate at the start will despawn once I leave that chunk but after that nothing happens, my main sphere which contains the script and the shader/material is the only thing that will remain in the scene. Im going to put my code below and as a file, and mind you I’m clueless, all of that was generated by the AI, I’m not expecting to have my handheld im just curious and very interested in what everyone thinks and if there is a possible solution to this. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StarGenerator : MonoBehaviour
{
public Material starMaterial;
public Shader starShader;
public int starCountPerChunk = 10;
public float minStarSpacing = 1.5f;
public float minStarSize = 0.5f;
public float maxStarSize = 1.5f;
public float maxViewDistance = 100.0f;
public float chunkDistance = 50.0f;
private List<GameObject> stars = new List<GameObject>();
private Vector3 lastChunkPosition;
void Start()
{
// Generate the first chunk of stars at the start position
lastChunkPosition = transform.position;
GenerateStars();
}
void Update()
{
float cameraDistance = Vector3.Distance(Camera.main.transform.position, transform.position);
// Check if the camera has moved to a new chunk of stars
if (cameraDistance > chunkDistance && Vector3.Distance(lastChunkPosition, transform.position) > chunkDistance)
{
lastChunkPosition = transform.position;
GenerateStars();
}
// Despawn stars that are beyond maxViewDistance from the camera
if (stars.Count > 0)
{
for (int i = stars.Count - 1; i >= 0; i--)
{
if (Vector3.Distance(Camera.main.transform.position, stars[i].transform.position) > maxViewDistance)
{
Destroy(stars[i]);
stars.RemoveAt(i);
}
}
}
}
private void GenerateStars()
{
// Create a new game object for each star in the chunk
for (int i = 0; i < starCountPerChunk; i++)
{
// Create a new star game object with a sphere mesh and material
GameObject star = GameObject.CreatePrimitive(PrimitiveType.Sphere);
star.name = "Star";
star.GetComponent<Renderer>().material = starMaterial;
star.GetComponent<Renderer>().material.shader = starShader;
// Randomly position the star within a grid
Vector3 randomPosition = GenerateRandomPosition();
while (IsPositionTooCloseToExistingStars(randomPosition))
{
randomPosition = GenerateRandomPosition();
}
star.transform.position = randomPosition;
// Set the scale of the star to a random size
float scale = Random.Range(minStarSize, maxStarSize);
star.transform.localScale = new Vector3(scale, scale, scale);
// Randomly change the BaseColor and CellColor of the star material
Color baseColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
Color cellColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
star.GetComponent<Renderer>().material.SetColor("_BaseColor", baseColor);
star.GetComponent<Renderer>().material.SetColor("_CellColor", cellColor);
// Add the star to the list of stars
stars.Add(star);
}
}
private Vector3 GenerateRandomPosition()
{
// Generate a random position within a box around the center of the generator
float x = Random.Range(-minStarSpacing, minStarSpacing) + Camera.main.transform.position.x;
float y = Random.Range(-minStarSpacing, minStarSpacing) + Camera.main.transform.position.y;
float z = Random.Range(-minStarSpacing, minStarSpacing) + Camera.main.transform.position.z;
return new Vector3(x, y, z);
}
private bool IsPositionTooCloseToExistingStars(Vector3 position)
{
// Check if the position is too close to any existing stars
foreach (GameObject star in stars)
{
if (Vector3.Distance(position, star.transform.position) < minStarSpacing)
{
return true;
}
}
return false;
}
}
8933166–1224729–StarGenerator.cs (3.98 KB)