using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoonOrbitPluto : MonoBehaviour { public Transform pluto; public PlutoMoonType moonType = PlutoMoonType.Charon; public bool autoCalculateOrbit = true; public float manualOrbitRadius = 10f; public float orbitInclination = 0.08f; public float startAngleDegrees = 0f; public float timeScale = 10f; public bool showDebugInfo = false; private const float PLUTO_RADIUS_KM = 1188f; private const float CHARON_RADIUS_KM = 606f; private const float CHARON_DISTANCE_KM = 19591f; private const float CHARON_PERIOD_HOURS = 153.3f; private float orbitRadius; private float angularSpeed; private float currentAngle = 0f; private float orbitCount = 0f; private float debugTimer = 0f; public enum PlutoMoonType { Charon } void Start() { if (pluto == null) { pluto = GameObject.Find("Pluto")?.transform; if (pluto == null) { enabled = false; return; } } CalculateOrbit(); SetInitialPosition(); } void CalculateOrbit() { float plutoRadius = pluto.localScale.x / 2f; if (autoCalculateOrbit) { switch (moonType) { case PlutoMoonType.Charon: orbitRadius = plutoRadius * (CHARON_DISTANCE_KM / PLUTO_RADIUS_KM); angularSpeed = (2f * Mathf.PI / (CHARON_PERIOD_HOURS * 3600f)) * timeScale; orbitInclination = 0.08f; break; } } else { orbitRadius = manualOrbitRadius; float period = GetPeriod() * 3600f; angularSpeed = (2f * Mathf.PI / period) * timeScale; } } float GetPeriod() { switch (moonType) { case PlutoMoonType.Charon: return CHARON_PERIOD_HOURS; default: return CHARON_PERIOD_HOURS; } } void SetInitialPosition() { currentAngle = startAngleDegrees * Mathf.Deg2Rad; UpdatePosition(); } void Update() { if (pluto == null) return; currentAngle += angularSpeed * Time.deltaTime; if (currentAngle >= 2f * Mathf.PI) { currentAngle -= 2f * Mathf.PI; orbitCount++; if (showDebugInfo) { Debug.Log(moonType + " orbit " + orbitCount); } } UpdatePosition(); if (showDebugInfo) { debugTimer += Time.deltaTime; if (debugTimer >= 10f) { debugTimer = 0f; float progress = (currentAngle / (2f * Mathf.PI)) * 100f; Debug.Log(moonType + ": R=" + orbitRadius.ToString("F2") + " P=" + progress.ToString("F1") + "%"); } } } 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 = pluto.position + new Vector3(x, y, z); } void OnDrawGizmos() { if (pluto == null) return; Color gizmoColor = new Color(0.7f, 0.7f, 0.7f, 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 pluto.position + new Vector3(x, y, z); } }