Hello, I’m attempting to use linerenderer or Debug.Drawline to “connect” GameObjects that are randomly placed near each other. I am not sure how to make this work, as I am not sure if Physics.OverlapSphere or Physics.SphereCast should be used to find the locations of the nearby game objects. Below is my script for the instantiation and the attempt at line drawing that is not working.
These are my functions: (CreateStar selects the prefab to be instantiated, StarGen find the position and places the star, and CreateTravelRoutes is supposed to connect nearby stars to each other) I can’t get this last function to work. Any help would be greatly appreciated!!
GameObject CreateStar(Star starData, Vector3 starPos)
{
float starGenPercentage = Random.value;
if (starGenPercentage <= .1)
{
GameObject starObj = Instantiate(starM);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
else if (starGenPercentage > .1 && starGenPercentage <= .55)
{
GameObject starObj = Instantiate(starK);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
else if (starGenPercentage > .55 && starGenPercentage <= .70)
{
GameObject starObj = Instantiate(starF);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
else if (starGenPercentage > .70 && starGenPercentage <= .85)
{
GameObject starObj = Instantiate(starG);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
else if (starGenPercentage > .85 && starGenPercentage <= .95)
{
GameObject starObj = Instantiate(starA);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
else
{
GameObject starObj = Instantiate(starB);
starObj.name = starData.starName;
starObj.transform.position = starPos;
return starObj;
}
}
public void StarGen()
{
int failCount = 0;
float mapLength = mapDimensions;
float mapHeight = mapDimensions;
float mapDepth = mapDimensions;
for (int i = 0; i < starCount; i++)
{
//Giving stars names upon generation
Star starData = new Star("Star" + i, Random.Range(1, 10));
Debug.Log("Created " + starData.starName + " with " + starData.planetCount + " number of planets.");
//Generating Star Positions, based on a center point of (0,0,0)
Vector3 starPos = new Vector3(Random.Range(-mapLength / 2, mapLength / 2), Random.Range(-mapHeight / 2, mapHeight / 2), Random.Range(-mapDepth / 2, mapDepth / 2));
//Setting a collider to be used so that stars are not generated on top of each other
Collider[] posCollider = Physics.OverlapSphere(starPos, minStarDist);
if (posCollider.Length == 0)
{
GameObject starObj = CreateStar(starData, starPos);
CreateTravelRoutes(starPos);
starList.Add(starObj);
}
else
{
i--;
failCount++;
}
if (failCount > starCount)
{
break;
}
}
}
void CreateTravelRoutes(Vector3 starPos)
{
List<GameObject> starsList = starList;
Transform origin;
Transform destination;
foreach (GameObject starObj in starsList)
{
if (Physics.SphereCast(starObj.transform.position, maxTravelDist, transform.forward, out RaycastHit hit, 0.1f) == true)
{
origin = starObj.transform;
destination = hit.rigidbody.gameObject.transform;
Debug.DrawLine(origin.position, destination.position, Color.white, 100);
//LineRenderer lineRenderer = GetComponent<LineRenderer>();
//lineRenderer.SetPosition(0, origin.position);
//lineRenderer.SetPosition(1, destination.position);
}
}
}