Last active
November 2, 2018 01:37
-
-
Save stipebosnjak/76a280ceb91e40238c4bf72782d8bc7a to your computer and use it in GitHub Desktop.
Planet orbit - Stash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public class PlanetOrbit : MonoBehaviour | |
{ | |
public static float EARTH_DISTANCE = 4000f; | |
public static int TimeMultiplier = 200; | |
public enum Orbit | |
{ | |
Auto, | |
Manual | |
} | |
public enum Rotation | |
{ | |
Auto, | |
Manual | |
} | |
public enum Season | |
{ | |
Auto, | |
Manual | |
} | |
public string Name = ""; | |
public Transform Parent; | |
public Orbit SetOrbit; | |
public Rotation SetRotation; | |
public Season SetSeason; | |
public bool TidalLock; | |
public bool LockOrbit; | |
public float OrbitAngle; | |
public bool KeepTime; | |
//private Transform transform; | |
private float EarthDays = 365.242199f; | |
public float OrbitalPeriodFactor = 1.0f; | |
public float DistanceFactor = 1.0f; | |
public float ScaleFactor = 1.0f; | |
// Orbit Stats | |
public float OrbitalPeriod = 1.0f; // Earth Years | |
public float OrbitalDistance = 2; | |
public Vector3 OrbitOffset; | |
public float OrbitPosOffset; | |
public float OrbitStartPos; | |
public float OrbitYears; | |
public float OrbitDays; | |
public float OrbitHours; | |
public float OrbitMinutes; | |
public float OrbitSeconds; | |
private float OrbitalTime; | |
private float OrbitalDegSec; | |
//Rotation Stats | |
public float RotationOffset; | |
public float RotationPeriod; // Earth Hours | |
public int RotationYears; | |
public int RotationDays; | |
public int RotationHours; | |
public int RotationMinutes; | |
public float RotationSeconds; | |
private float RotationDegSec; | |
private float RotationTime; | |
// Planetary Stats | |
public float AxialTilt; | |
public int HoursInDay; | |
public int RotInOrbit; | |
//Planet Counters | |
public int CounterYear; | |
public int CounterDay; | |
public int CounterHour; | |
public int CounterMinute; | |
public float CounterSecond; | |
public float CurrentOrbitPos; | |
public bool OrbitOffSetYear; | |
private float RotCounter; | |
private float OrbCounter; | |
//Draw Orbit | |
public bool _DrawOrbit = false; | |
public float DisplaySize = 0.05f; | |
public Color DisplayColor = Color.blue; | |
public int Segments = 100; | |
public Texture2D DisplayTexture; | |
public int DisplayTiling = 50; | |
public bool UseTexture; | |
private Transform ThisOrbit; | |
private LineRenderer LR; | |
public Transform OrbitCenter; | |
public string Hash { get { return Math.Abs(Name.GetHashCode()).ToString(); } } | |
void Awake() | |
{ | |
if (!Parent) | |
Debug.LogError(string.Format("Parent mass not specified! - Name:{0}", Name)); | |
GameObject earthGo; | |
if (earthGo = GameObject.Find("Earth")) | |
{ | |
Debug.Log(ScaleFactor + " " + earthGo.transform.localScale); | |
transform.localScale = earthGo.transform.localScale * ScaleFactor; | |
} | |
else | |
Debug.LogError("No planet named Earth, please specify one"); | |
OrbitalDistance = EARTH_DISTANCE * DistanceFactor; | |
OrbitYears = (EarthDays * OrbitalPeriodFactor) / 365f; | |
Debug.Log(string.Format("Setting planet orbit - Name:{0} OrbitalDistance:{1} OrbitYears:{2} ScaleFactor:{3}", Name, OrbitalDistance, OrbitalPeriod, ScaleFactor)); | |
transform.localEulerAngles = new Vector3( | |
transform.localEulerAngles.x, | |
transform.localEulerAngles.y, | |
AxialTilt); | |
OrbitCenter = new GameObject(Name + "OrbitCenter").transform; | |
OrbitCenter.position = Parent.position; | |
transform.parent = OrbitCenter; | |
transform.localPosition = new Vector3(0, 0, OrbitalDistance); | |
OrbitCenter.eulerAngles = | |
new Vector3( | |
OrbitAngle, | |
transform.eulerAngles.y, | |
transform.eulerAngles.z); ; | |
OrbitCenter.Rotate(0, OrbitPosOffset, 0); | |
if (_DrawOrbit) | |
{ | |
if (GameObject.Find("Orbits") == null) | |
{ | |
GameObject Orbits = new GameObject(Name + "Orbits"); | |
Orbits.transform.position = Vector3.zero; | |
Orbits.transform.eulerAngles = Vector3.zero; | |
} | |
} | |
} | |
void Start() | |
{ | |
SetupPlanet(); | |
if (OrbitOffSetYear) | |
{ | |
OrbCounter = OrbitPosOffset; | |
} | |
if (LockOrbit) | |
{ | |
KeepTime = false; | |
} | |
if (_DrawOrbit) | |
{ | |
SetupDrawOrbit(); | |
} | |
} | |
void SetupDrawOrbit() | |
{ | |
GameObject Orbit = new GameObject(Name + "Orbit_Path"); | |
Orbit.transform.eulerAngles = new Vector3( | |
OrbitAngle, | |
transform.eulerAngles.y, | |
transform.eulerAngles.z); ; | |
Orbit.transform.parent = GameObject.Find(Name + "Orbits").transform; | |
Orbit.transform.position = Parent.position; | |
Orbit.AddComponent<LineRenderer>(); | |
LR = Orbit.GetComponent<LineRenderer>(); | |
LR.SetWidth(DisplaySize, DisplaySize); | |
LR.material.shader = Shader.Find("Particles/Additive"); | |
LR.material.SetColor("_TintColor", DisplayColor); | |
LR.useWorldSpace = false; | |
LR.SetVertexCount(Segments + 1); | |
if (DisplayTexture != null) | |
{ | |
LR.material.mainTexture = DisplayTexture; | |
// LR.material.mainTextureScale.x = DisplayTiling; | |
} | |
ThisOrbit = Orbit.transform; | |
float Angle = 0; | |
for (int i = 0; i < (Segments + 1); i++) | |
{ | |
Vector2 NewRadius = new Vector2(Mathf.Sin(Mathf.Deg2Rad * Angle) * OrbitalDistance, | |
Mathf.Cos(Mathf.Deg2Rad * Angle) * OrbitalDistance); | |
LR.SetPosition(i, new Vector3(NewRadius.y, 0, NewRadius.x)); | |
Angle += (360.0f / Segments); | |
} | |
} | |
void SetupPlanet() | |
{ | |
//Setup Orbit Time | |
if (SetOrbit == 0) | |
{ | |
OrbitalTime = ((((EarthDays * OrbitalPeriod) * 24) * 60) * 60); | |
OrbitalDegSec = (360 / OrbitalTime) * TimeMultiplier; | |
} | |
else | |
{ | |
OrbitalPeriod = 0; | |
OrbitalTime = ((((((((OrbitYears * EarthDays) + OrbitDays) * 24) + OrbitHours) * 60) + OrbitMinutes) * 60) + OrbitSeconds); | |
OrbitalDegSec = (360 / OrbitalTime) * TimeMultiplier; | |
} | |
//Setup Rotation Time | |
if (!TidalLock) | |
{ | |
if (SetRotation == 0) | |
{ | |
RotationTime = (((24 * RotationPeriod) * 60) * 60); | |
RotationDegSec = (360 / OrbitalTime) * TimeMultiplier; | |
} | |
else | |
{ | |
RotationPeriod = 0; | |
RotationTime = ((((((((RotationYears * EarthDays) + RotationDays) * 24) + RotationHours) * 60) + RotationMinutes) * 60) + RotationSeconds); | |
} | |
RotationDegSec = (360 / RotationTime) * TimeMultiplier; | |
RotInOrbit = (int)Mathf.Round(OrbitalTime / RotationTime); | |
HoursInDay = (int)((RotationTime / 60) / 60); | |
} | |
} | |
void Update() | |
{ | |
var ods = 0f; | |
if (!LockOrbit) | |
{ | |
ods = OrbitalDegSec * Time.deltaTime; | |
OrbitStartPos += ods; | |
OrbitCenter.Rotate(0, ods, 0); | |
} | |
// Update Rotation | |
if (TidalLock) | |
{ | |
transform.LookAt(Parent); | |
if (KeepTime) | |
{ | |
UpdateCounters(0, ods); | |
} | |
} | |
else | |
{ | |
float RotDegSec = RotationDegSec * Time.deltaTime; | |
if (KeepTime) | |
{ | |
UpdateCounters(RotDegSec, ods); | |
} | |
transform.Rotate(0, RotDegSec, 0, Space.Self); | |
} | |
} | |
void UpdateCounters(float RotDegSec, float ods) | |
{ | |
//Count Orbits / Years | |
if ((OrbCounter + ods) >= 360) | |
{ | |
CounterYear += 1; | |
CounterDay = 0; | |
OrbCounter = (OrbCounter + ods) - 360; | |
} | |
else | |
{ | |
OrbCounter += ods; | |
} | |
CurrentOrbitPos = OrbCounter; | |
//Count Days | |
if ((RotCounter + RotDegSec) >= 360) | |
{ | |
CounterDay += 1; | |
RotCounter = (RotCounter + RotDegSec) - 360; | |
} | |
else | |
{ | |
RotCounter += RotDegSec; | |
} | |
var CurrentTime = (RotCounter / 360) * RotationTime; | |
//Count Hours | |
CounterHour = (int)(CurrentTime / 60) / 60; | |
//Count Minutes | |
if (CounterHour > 0) | |
{ | |
CounterMinute = (int)(CurrentTime / 60) - (CounterHour * 60); | |
} | |
else | |
{ | |
CounterMinute = (int)(CurrentTime / 60); | |
} | |
//Count Seconds | |
if (CounterHour > 0 && CounterMinute > 0) | |
{ | |
CounterSecond = CurrentTime - ((CounterMinute + (CounterHour * 60)) * 60); | |
} | |
else if (CounterHour > 0 && CounterMinute == 0) | |
{ | |
CounterSecond = CurrentTime - ((CounterHour * 60) * 60); | |
} | |
else if (CounterHour == 0 && CounterMinute > 0) | |
{ | |
CounterSecond = CurrentTime - (CounterMinute * 60); | |
} | |
else if (CounterHour == 0 && CounterMinute == 0) | |
{ | |
CounterSecond = CurrentTime; | |
} | |
} | |
void LateUpdate() | |
{ | |
Vector3 CurPos = Parent.position + new Vector3(OrbitOffset.x, OrbitOffset.y, OrbitOffset.z); | |
OrbitCenter.position = CurPos; | |
if (_DrawOrbit) | |
{ | |
ThisOrbit.position = CurPos; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment