Hey, I’ve got this working code, lets say you visit the 1st planet, even thou it is very close to like 300 unit distance, it is very jumpy, it moves then kinda micro jumps out at you then moves back into position and keeps moving and does the same thing all over again. I’ve heard of adding Lerp to Mathf, but no idea where to add it to. since I already have sin and cos where usually Lerp goes.
using UnityEngine;
using System.Collections.Generic;
public class OrbitEllipse : MonoBehaviour {
public List<int> aphelionX = new List<int >(new int [] {0,398,549,752,1149,2820,4435,6363,7370,37610}); // Orbit Distance List
public List<int> perihelionY = new List<int >(new int [] {0,260,530,747,1106,1407,3098,4649,6597, 6370}); // Orbit Distance List
public float speed = 1; // Orbit Speed
public float MaximumSizeX = 1; // Ellipse Maximum Size
public float MinimumSizeY = 1; // Ellipse Minimum Size
private float alpha;
private float locX = 0;
private float locY = 0;
private float X;
private float Y;
private int Name;
void Start () {
Name = int.Parse(transform.gameObject.name);
transform.localPosition = new Vector3(0,0,0);
MaximumSizeX = aphelionX[Name];
MinimumSizeY = perihelionY[Name];
}
// Update is called once per frame
void Update () {
alpha += 10 ;
X = locX + (MaximumSizeX * Mathf.Cos(alpha*speed/100000));
Y = locY + (MinimumSizeY * Mathf.Sin(alpha*speed/100000));
this.gameObject.transform.localPosition = new Vector3(X,0,Y);
}
}
I’ve been playing with this for a little while now. Pretty fun. It took a while to get the ‘smoothness’ that you were talking about. I think it’s pretty good. I have a scene that I’ve been toying with it in and, by placing differently scaled objects around the orbits of these suckers, it seems passable to me. Then again, I don’t know what scales you’ll be working at, so maybe it’ll still be too jerky for you too? Let me know.
Oh yeah…I pretty much rewrote the whole thing. Now you just place it on an empty GameObject, and configure the Solar System.
using UnityEngine;
using System.Collections;
public class OrbitEllipse : MonoBehaviour
{
const float VELOCITY_MULTIPLIER = 0.1f;
public bool showTrails;
public SolarSystem solarSystem;
void Awake()
{
Material trailMaterial = null;
if (showTrails)
trailMaterial = Resources.LoadAssetAtPath<Material>("Assets/UA/Materials/CelestialBodyTrail.mat");
foreach (CelestialBody body in solarSystem.celestialBodies)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Transform t = go.transform;
t.parent = transform;
body.transform = t;
t.localPosition = GetPosition(body);
t.localScale *= body.size;
t.name = body.name;
if (showTrails)
{
TrailRenderer trailRenderer = t.gameObject.AddComponent<TrailRenderer>();
trailRenderer.material = trailMaterial;
trailRenderer.material.color = body.trailColor;
}
if (body.orbitSpeed > 0)
StartCoroutine(UpdateBody(body));
}
}
IEnumerator UpdateBody(CelestialBody body)
{
while (body.transform)
{
body.transform.localPosition = Vector3.Slerp(body.transform.position, GetPosition(body), Time.deltaTime);
yield return null;
}
}
Vector3 GetPosition(CelestialBody body)
{
float k = Time.time * body.orbitSpeed * VELOCITY_MULTIPLIER;
float x = body.aphelion * Mathf.Cos(k);
float y = body.perihelion * Mathf.Sin(k);
return new Vector3(x,0,y);
}
}
[System.Serializable]
public class SolarSystem
{
public CelestialBody[] celestialBodies;
}
[System.Serializable]
public class CelestialBody
{
public string name;
public float size;
public float aphelion;
public float perihelion;
public float orbitSpeed;
public Color trailColor;
[HideInInspector]
public Transform transform;
}
If you really want to pre-initialize all of your values then do so in the Reset() method.