private void Awake() { other = orbitted.GetComponent <Rigidbody2D>(); rotSpeed = Random.Range(-15, 15); ellip = Random.Range(0, 10); }
/* * * private List<GameObject> planets = new List<GameObject>(); * * void Start() * { * StartCoroutine(SpawnPlanets()); * * sun.GetComponent<Rigidbody2D>().velocity = sunVelocity; * } * * private IEnumerator SpawnPlanets() * { * for (int i = 0; i < numberOfPlanets; i++) * { * GameObject planet = Instantiate(planetPrefab); * float x = 5; * float y = 5; * * * //Add below back when fixed the limits on random spawn location * ///float x = -radius / 2f + Random.value * radius; * ///float y = -radius / 2f + Random.value * radius; * /// * * * Vector2 position = new Vector2(x, y); * planet.transform.position = position; * float scale = 1f + Random.value * maxPlanetSize; * planet.transform.localScale = new Vector2(scale, scale); * * float m = minPlanetMass + Random.value * (maxPlanetMass - minPlanetMass); * Rigidbody2D prb = planet.GetComponent<Rigidbody2D>(); * prb.mass = m; * * //Static setting of velocity * prb.velocity = new Vector2(3, 0); * * planets.Add(planet); * * yield return new WaitForSeconds(spawnDelay); * } * } * */ static public Gravity SpawnNewPlanet(Vector2 position, GameObject prefab) { allcol[0] = new Color(0, 0, 0, 1); allcol[1] = new Color(0, 0, 1, 1); allcol[2] = new Color(0, 1, 1, 1); allcol[3] = new Color(0.5f, 0.5f, 0.5f, 1); allcol[4] = new Color(0, 1, 0, 1); allcol[5] = new Color(1, 0, 1, 1); allcol[6] = new Color(1, 0, 0, 1); allcol[7] = new Color(1, 0.92f, 0.016f, 1); allcol[8] = new Color(1, 1, 1, 1); GameObject planet = Instantiate(prefab); planettemp = prefab.tag; Gravity g = planet.GetComponentInChildren <Gravity>(); //RigidBody2D rbody = planet.GetComponentInChildren<Rigidbody2D>(); planet.transform.position = position; g.transform.position = position; g.GetComponent <Rigidbody2D>().position = position; float m = minPlanetMass + (Random.value * (maxPlanetMass - minPlanetMass)); if (prefab.CompareTag("Black Hole")) { m = 100 + Random.value * 1000; } g.GetComponent <Rigidbody2D>().mass = m; float scale = 1f + Random.value * maxPlanetSize; planet.transform.localScale = new Vector2(scale, scale); Vector2 toCenter = GameManager.Instance.deltaV(g); float distToCenter = (GameManager.Instance.CenterOfSystem() - position).magnitude; g.startVelocity = Quaternion.Euler(0, 0, 90) * toCenter.normalized * orbitConstant / Mathf.Sqrt(distToCenter); temp = allcol[(int)Mathf.Round(Random.Range(0, 8))]; planet.GetComponentInChildren <SpriteRenderer>().color = temp; return(g); }
/// <summary> /// Gets the planet VertexNavigation instance. /// </summary> /// <returns>The planet navigation.</returns> public VertexNavigation getPlanetNavigation() { InterplanetaryObject ipo = this.GetComponent <InterplanetaryObject>(); Gravity grav = ipo.NearestPlanet; VertexNavigation vertNav = grav.GetComponent <VertexNavigation>(); return(vertNav); // return (VertexNavigation) this.GetComponent<InterplanetaryObject>().NearestPlanet.GetComponent<VertexNavigation>(); }
public void Update() { foreach (KeyValuePair <Gravity, GameObject> keyValue in planetControllers) { Gravity g = keyValue.Key; GameObject pc = keyValue.Value; UIControllerSetup ui = pc.GetComponent <UIControllerSetup>(); ui.massText.GetComponent <Text>().text = "Mass: " + Mathf.Round(g.GetComponent <Rigidbody2D>().mass); ui.posXText.GetComponent <Text>().text = "X: " + Mathf.Round(g.GetComponent <Rigidbody2D>().position.x * 100); ui.posYText.GetComponent <Text>().text = "Y: " + Mathf.Round(g.GetComponent <Rigidbody2D>().position.y * 100); ui.velocityXText.GetComponent <Text>().text = "Vx: " + Mathf.Round(g.GetComponent <Rigidbody2D>().velocity.x * 100); ui.velocityYText.GetComponent <Text>().text = "Vy: " + Mathf.Round(g.GetComponent <Rigidbody2D>().velocity.y * 100); } if (physObjects.Count > 0) { CameraReposition(); } }
public Vector2 deltaV(Gravity obj) { Vector2 c = Vector2.zero; foreach (Gravity go in physObjects) { Vector2 diff = obj.GetComponent <Rigidbody2D>().position - go.GetComponent <Rigidbody2D>().position; float rsq = diff.sqrMagnitude; if (go != obj) { c += go.GetComponent <Rigidbody2D>().mass *diff.normalized / rsq; } } return(G * c * Time.fixedDeltaTime); }
void Attract(Gravity other) { Rigidbody2D rbOther = other.GetComponent <Rigidbody2D>(); Vector3 direction = rig.position - rbOther.position; float distance = direction.magnitude; if (distance == 0) { return; } float forceMagnitude = G * (rig.mass * rbOther.mass) / Mathf.Pow(distance, 2); Vector3 force = direction.normalized * forceMagnitude; rbOther.AddForce(force); }
public void setup(Gravity g, string name) { grav = g; massLabel.GetComponent <Text>().text = name; Slider mSlider = massSlider.GetComponent <Slider>(); mSlider.value = Mathf.Sqrt(g.GetComponent <Rigidbody2D>().mass); mSlider.onValueChanged.AddListener(f => g.changeMass(f * f)); massInputField.GetComponent <InputField>().onEndEdit.AddListener(f => g.changeMass(float.Parse(f))); posXInputField.GetComponent <InputField>().onEndEdit.AddListener(f => g.changePosX(float.Parse(f) / 100)); posYInputField.GetComponent <InputField>().onEndEdit.AddListener(f => g.changePosY(float.Parse(f) / 100)); velocityXInputField.GetComponent <InputField>().onEndEdit.AddListener(f => g.changeVelocityX(float.Parse(f) / 100)); velocityYInputField.GetComponent <InputField>().onEndEdit.AddListener(f => g.changeVelocityY(float.Parse(f) / 100)); trailLabel.GetComponent <Text>().text = "Length of Trail"; Slider tSlider = trailSlider.GetComponent <Slider>(); tSlider.value = 5; tSlider.onValueChanged.AddListener(l => g.changeTrailLength(l)); }