Initial commit
This commit is contained in:
164
Assets/Scripts/MoonOrbitNeptune.cs
Normal file
164
Assets/Scripts/MoonOrbitNeptune.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoonOrbitNeptune : MonoBehaviour
|
||||
{
|
||||
public Transform neptune;
|
||||
|
||||
public NeptuneMoonType moonType = NeptuneMoonType.Triton;
|
||||
public bool autoCalculateOrbit = true;
|
||||
public float manualOrbitRadius = 10f;
|
||||
|
||||
public float orbitInclination = 156.8f;
|
||||
public float startAngleDegrees = 0f;
|
||||
|
||||
public float timeScale = 10f;
|
||||
|
||||
public bool showDebugInfo = false;
|
||||
|
||||
private const float NEPTUNE_RADIUS_KM = 24622f;
|
||||
|
||||
private const float TRITON_RADIUS_KM = 1353f;
|
||||
private const float TRITON_DISTANCE_KM = 354759f;
|
||||
private const float TRITON_PERIOD_HOURS = 141.0f;
|
||||
|
||||
private float orbitRadius;
|
||||
private float angularSpeed;
|
||||
private float currentAngle = 0f;
|
||||
private float orbitCount = 0f;
|
||||
private float debugTimer = 0f;
|
||||
|
||||
public enum NeptuneMoonType
|
||||
{
|
||||
Triton
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (neptune == null)
|
||||
{
|
||||
neptune = GameObject.Find("Neptune")?.transform;
|
||||
if (neptune == null)
|
||||
{
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
CalculateOrbit();
|
||||
SetInitialPosition();
|
||||
}
|
||||
|
||||
void CalculateOrbit()
|
||||
{
|
||||
float neptuneRadius = neptune.localScale.x / 2f;
|
||||
|
||||
if (autoCalculateOrbit)
|
||||
{
|
||||
switch (moonType)
|
||||
{
|
||||
case NeptuneMoonType.Triton:
|
||||
orbitRadius = neptuneRadius * (TRITON_DISTANCE_KM / NEPTUNE_RADIUS_KM);
|
||||
angularSpeed = -(2f * Mathf.PI / (TRITON_PERIOD_HOURS * 3600f)) * timeScale;
|
||||
orbitInclination = 156.8f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
orbitRadius = manualOrbitRadius;
|
||||
float period = GetPeriod() * 3600f;
|
||||
angularSpeed = -(2f * Mathf.PI / period) * timeScale;
|
||||
}
|
||||
}
|
||||
|
||||
float GetPeriod()
|
||||
{
|
||||
switch (moonType)
|
||||
{
|
||||
case NeptuneMoonType.Triton: return TRITON_PERIOD_HOURS;
|
||||
default: return TRITON_PERIOD_HOURS;
|
||||
}
|
||||
}
|
||||
|
||||
void SetInitialPosition()
|
||||
{
|
||||
currentAngle = startAngleDegrees * Mathf.Deg2Rad;
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (neptune == null) return;
|
||||
|
||||
currentAngle += angularSpeed * Time.deltaTime;
|
||||
|
||||
if (currentAngle <= -2f * Mathf.PI)
|
||||
{
|
||||
currentAngle += 2f * Mathf.PI;
|
||||
orbitCount++;
|
||||
|
||||
if (showDebugInfo)
|
||||
{
|
||||
Debug.Log(moonType + " orbit " + orbitCount + " (retrograde)");
|
||||
}
|
||||
}
|
||||
|
||||
UpdatePosition();
|
||||
|
||||
if (showDebugInfo)
|
||||
{
|
||||
debugTimer += Time.deltaTime;
|
||||
if (debugTimer >= 10f)
|
||||
{
|
||||
debugTimer = 0f;
|
||||
float progress = (Mathf.Abs(currentAngle) / (2f * Mathf.PI)) * 100f;
|
||||
Debug.Log(moonType + ": R=" + orbitRadius.ToString("F2") + " P=" + progress.ToString("F1") + "% RETROGRADE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePosition()
|
||||
{
|
||||
float x = Mathf.Cos(currentAngle) * orbitRadius;
|
||||
float z = Mathf.Sin(currentAngle) * orbitRadius;
|
||||
|
||||
float inclinationRad = orbitInclination * Mathf.Deg2Rad;
|
||||
float y = z * Mathf.Sin(inclinationRad);
|
||||
z = z * Mathf.Cos(inclinationRad);
|
||||
|
||||
transform.position = neptune.position + new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (neptune == null) return;
|
||||
|
||||
Color gizmoColor = new Color(0.4f, 0.7f, 1f, 0.5f);
|
||||
|
||||
Gizmos.color = gizmoColor;
|
||||
|
||||
int segments = 64;
|
||||
Vector3 prevPos = GetOrbitPoint(0);
|
||||
|
||||
for (int i = 1; i <= segments; i++)
|
||||
{
|
||||
Vector3 newPos = GetOrbitPoint(i * 2f * Mathf.PI / segments);
|
||||
Gizmos.DrawLine(prevPos, newPos);
|
||||
prevPos = newPos;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 GetOrbitPoint(float angle)
|
||||
{
|
||||
float x = Mathf.Cos(angle) * orbitRadius;
|
||||
float z = Mathf.Sin(angle) * orbitRadius;
|
||||
|
||||
float inclinationRad = orbitInclination * Mathf.Deg2Rad;
|
||||
float y = z * Mathf.Sin(inclinationRad);
|
||||
z = z * Mathf.Cos(inclinationRad);
|
||||
|
||||
return neptune.position + new Vector3(x, y, z);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user