Moving a GameObject in an Ellipsoid path
I'm aware this question has been asked twice before but for some reason the code that the users provide doesn't work for me and constantly returns compiling errors.
I'm not the best at C# so I'm still learning but here is my code. using UnityEngine; using System.Collections;
public class Elliptical : MonoBehaviour
{
public float x;
public float y;
public float a = 215f; //a is in AU, Semimajor Axis
public float e = 0.0167f; //eccentricity of the planet
private float b; // defined in Update(), Semiminor Axis
private float r; // r
private float R; // R = r'
private float alpha; // angle theta
// Update is called once per frame
void Update()
{
var Sun = GameObject.Find("Sun");
b = Mathf.Sqrt(Mathf.Pow(a, 2) * (1 - Mathf.Pow(e, 2))); // Finding the Semiminor Axis
var alpha = 0f;
var X = 0f;
var Y = 0f;
alpha += 10;
X = x + (a * Mathf.Cos(alpha * 0.005f));
Y = y + (b * Mathf.Sin(alpha * 0.005f));
this.gameObject.transform.position = Sun.transform.position + Vector3(X, 0, Y);
}
}
The compiling error I get is "Expression denotes a 'type', where a 'variable', or 'value' or 'method group' was expected" .
however when I change the last line to this.gameObject.transform.position = Sun.transform.position + new Vector3(X, 0, Y);
my code compiles but doesn't do what I want. Any help would be appreciated!
Answer by Nexusty · Jun 05, 2016 at 10:34 PM
Figured it out, posting for the sake of other people. code:
using UnityEngine;
using System.Collections;
public class what : MonoBehaviour
{
public float a = 215; //a is in AU, Semimajor Axis
private float angle; // angle theta
float speed = (2 * Mathf.PI) / 10;
float x;
float y;
float e = 0.9f; // Eccentricity
// Update is called once per frame
void Update()
{
float b = Mathf.Sqrt(Mathf.Pow(a, 2) * (1 - Mathf.Pow(e, 2))); // Finding the Semiminor Axis (sqrt);
var Planet = GameObject.Find("Planet");
angle += speed * Time.deltaTime;
x = Mathf.Cos(angle) * a; // a is the Radius in the x direction
y = Mathf.Sin(angle) * b; // b is the Radius in the y direction
Planet.transform.position = new Vector3(x, 0, y);
}
}
Note that this script is for only ellipsoid movement and does not accurately portray the movement of planets. To do this you'll have to use Newton's law of universal gravitation equation.