hi guys i am writing a solar system array how would i compare to see if i had an array of sun game objects the array can be a random length at the start of the game, how would i check to see if any of the suns are within a distance of each other i am a bit stuck heres the code so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystemCreator : MonoBehaviour {
public GameObject[] Suns;
public GameObject[] Planets;
public GameObject Moons;
public GameObject[] SunArray;
public GameObject[] PlanetArray;
//public GameObject[] MoonArray;
public int RandomSunArray;
public int RandomPlanetArray;
public int RandomMoonArray;
// Use this for initialization
public int CountFrames = 0;
void Awake () {
RandomSunArray = Random.Range(4,14);
RandomPlanetArray = Random.Range(8, 24);
RandomMoonArray = Random.Range(1, 4);
SunArray = new GameObject[RandomSunArray];
PlanetArray = new GameObject[RandomPlanetArray];
//MoonArray = new GameObject[RandomMoonArray];
for (int i = 0; i < SunArray.Length; i++)
{
///x,y,z//
int XPosiiton = Random.Range(300,-300);
int YPosiiton = 0;
int ZPosiiton = Random.Range(300, -300);
//x,y,z///
int PickARandomSun = Random.Range(0, 4);
SunArray *= Suns[PickARandomSun];*
// print(PickARandomSun);
SunArray = Instantiate(SunArray*);*
SunArray*.gameObject.transform.position = new Vector3(XPosiiton,YPosiiton,ZPosiiton);*
}
for (int i = 0; i < PlanetArray.Length; i++)
{
int PickARandomPlanet = Random.Range(0, 8);
PlanetArray = Planets[PickARandomPlanet];
// print(PickARandomSun);
}
// Destroy(Moons);
}
* // Update is called once per frame*
* void Update () {*
}
}
@raouligen
Here is a script that does just that but you are going to have to adjust it. By that I mean it gets the distance between the suns created. I could not understand what the rest of what you said. Hope it helps :D. Here it is:
public GameObject[] Suns;
GameObject[] SunArray;
public int RandomSunArray;
public int maxSuns = 4;
public int CountFrames = 0;
List<float> sunDistances;
List<string> fromSun;
List<string> toSun;
bool AllowedToPrint;
int PickARandomSun;
int XPosition;
int YPosition;
int ZPosition;
int sunCount;
void Start()
{
sunDistances = new List<float>();
fromSun = new List<string>();
toSun = new List<string>();
PickARandomSun = 0;
XPosition = 0;
YPosition = 0;
ZPosition = 0;
sunCount = 0;
}
private void Update()
{
if (AllowedToPrint == true)
{
PrintSunDistances();
AllowedToPrint = false;
}
if (sunCount < maxSuns)
{
RandomSunArray = Random.Range(0, 1); // Was 4 and 14
SunArray = new GameObject[maxSuns]; //RandomSunArray
for (int i = 0; i < SunArray.Length; i++)
{
Debug.Log("SunCount: " + sunCount);
sunCount++;
///x,y,z//
XPosition = Random.Range(20, -20); // Was -300 and 300
YPosition = 2; // Was 0
ZPosition = Random.Range(20, -20); // Was -300 and 300
//x,y,z///
PickARandomSun = Random.Range(0, 3); // was 0 and 4
// print(PickARandomSun);
SunArray *= Instantiate(Suns[PickARandomSun], new Vector3(XPosition, YPosition, ZPosition), transform.rotation);*
SunArray*.name = “Sun” + i;*
if (SunArray != SunArray[0] && SunArray != null && SunArray[i - 1] != null)
{
sunDistances.Add(Vector3.Distance(SunArray*.transform.position, SunArray[i - 1].transform.position));*
fromSun.Add(SunArray*.name);*
toSun.Add(SunArray[i - 1].name);
}
}
AllowedToPrint = true;
}
}
void PrintSunDistances()
{
for (int i = 0; i < sunDistances.Count; i++)
{
Debug.Log("Distance " + fromSun + " to " + toSun + " is " + sunDistances*);*
}
}
Yes except Y because at this stage at least I’m trying to keep everything linnear