- Home /
Simple Planet orbit
Hi,
Can I get a simple script for movement of an planet in an orbit. I tried quite a few scripts, but as I am not an very gud programmer I had some trouble dealing with the errors.
Please provide me with an simple orbit script for a planet revolving around an Star.
Avichal Singh
Dsksupinfogame.
Answer by e-bonneville · Mar 31, 2011 at 02:16 PM
I'm not sure why I'm writing a script for you, but just follow these steps:
- Make an empty gameObject. Name it "CenterOfGravity" (or the equivalent in your language)
- Make the planet(s) you want to rotate a child of the CenterOfGravity gameObject.
- Copy the script at the bottom of this answer onto your CenterOfGravity gameObject.
- For realistic behavior, move the CenterOfGravity gameObject to the position of your star.
JS script to put on your CenterOfGravity gameObject:
var rotationSpeed = 5;
function Update() { transform.Rotate(0,rotationSpeed * Time.deltaTime,0); }
Or in C#:
float rotationSpeed = 5.0f;
void Update() { transform.Rotate(0,rotationSpeed*Time.deltaTime,0); }
Please note that the above scripts are untested and may contain errors
Now, asking for a script and actually getting somebody to write it for you isn't very common and overall isn't a very good idea, so please try to avoid this kind of question in the future. Instead, a better way to do it might be to actually start trying to write the script yourself, and then asking somebody here if you have a problem with it.
Also, if this answer works for you, please accept it as correct. :)
You could put the script on the star ins$$anonymous$$d of an empty game object
Yes, but then that would make the star rotate as well, and that might not be what the OP wants. It's just an unnecessary precaution on my part, I suppose.
Answer by DaveA · Mar 31, 2011 at 03:37 PM
You could also just use the Animation editor to do what Elliot says. Make the planet a child (of the star or hidden object at the star) and animate that parent's rotation.
How simple is simple? If you want the planet to rotate as well as revolve around the planet, in a perfectly circular orbit, but its axis tilted (as most are), in addition to what Elliot has said, you'd want to have the planet rotate around itself too. But that's not enough, because the axis of rotation is constant relative to the universe, not just itself. Meaning if you did simply that, the planet's axis would always point away from the star. So you'd have to make the planet a child of yet another gameobject (empty or otherwise invisible), and that gameobject would counter-rotate at the same speed as the revolution around the star. Or maybe script it to set world-rotation (universe rotation in this case) at a constant axis. If you want orbits not on Y axis or elliptical, or based ephemeris, ping me directly.
Well thanks ! Elliot and DaveA ! And yes asking for a script is not a good idea :P but thanks for the script and it worked wid animation too :D Thanks a ton !
Avichal Singh Dsksupinfogame.
Your answer
Follow this Question
Related Questions
Axial tilt and moons 1 Answer
Itween for curved orbital movement? 1 Answer
Simulating 2D planetary gravity and trajectpry using Unity 0 Answers
How to rotate a player when using Mario Galaxy style gravity? 5 Answers
draw planet orbit on solar system 0 Answers