I got this code from somewhere else, I want to implement Kepler's third law to make the simulation look more realistic, the simulation is a 2D satellite orbiting around a planet, the satellite(Planet) script here is :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Planet : MonoBehaviour {
private float alpha; //will use for cos & sin (as angle)
private float distance;//distance from center
private float Rotation;//rotation angle
private float currentTime;//Time start when Instantiated
//input values
public float speed;
public Vector2 RotationValues;
public Vector2 distanceValues;
public Vector2 sizeValues;
void Start () {
//Initialize with Random rotation & distance & size
//Random Rotation between -2 & 2
Rotation = Random.Range(RotationValues.x, RotationValues.y);
//Random distance between -1 & 2
distance = Random.Range(distanceValues.x, distanceValues.y);
//Random Size between 0.2f & 0.7f
var Size = Random.Range(sizeValues.x, sizeValues.y);
//Set localScale with random size
transform.localScale = new Vector3(Size, Size, Size);
//calculating current time by subtraction Time.time and lastTime
currentTime = Time.time - Generator.lastTime;
//GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value); //Random Color
}
void Update () {
//Timer
currentTime += Time.deltaTime;
var X = (distance * Mathf.Cos(currentTime)); //X
var Y = (distance * Mathf.Sin(currentTime)); //Y
var R = (Rotation * Mathf.Cos(currentTime)); //Rotation
//give earth from generator class
var Earth = Generator.Earth;
transform.position = Earth.transform.position + (new Vector3(X, R, Y));// RotateAround Earth
}
}
also I have this other script which generates the planets:
using UnityEngine;
using System.Collections;
public class Generator : MonoBehaviour {
public GameObject prefab;
public static GameObject Earth;
public static float lastTime;
public int numerOfSatellites;
void Start() {
Earth = GameObject.FindGameObjectWithTag("MainPlanet");//Find Earth
for (int i = 1; i <= numerOfSatellites; i++) {
lastTime = Time.time;
GameObject sat = Instantiate(prefab, Earth.transform.position, Quaternion.identity);
Physics2D.IgnoreCollision(sat.GetComponent<CircleCollider2D>(), Earth.GetComponent<CircleCollider2D>());
} }
}
I know that the main part here is
void Update () {
//Timer
currentTime += Time.deltaTime;
var X = (distance * Mathf.Cos(currentTime)); //X
var Y = (distance * Mathf.Sin(currentTime)); //Y
var R = (Rotation * Mathf.Cos(currentTime)); //Rotation
//give earth from generator class
var Earth = Generator.Earth;
transform.position = Earth.transform.position + (new Vector3(X, R, Y));// RotateAround Earth
}
The main problem is that whatever distance the planets have from the mainPlanet (called earth) they move at the same angular velocity, how would I change this?
Those are the basic 4 steps of every orbit, the problem here is that they all align in the center, which shouldn't be possible because of the random distances they have