How should I follow an object while that object orbits another object?
I'm dabbling in game development in order to actually enjoy programming outside of work. I also love space, and these two things naturally work well together. To learn Unity (my first foray into game dev since XNA was actively maintained), I've decided to try and build a simple 2D orbital simulation. The idea is thus, one star, one planet, and one moon. The planet orbits the star in a perfectly circular orbit, and the moon orbits the planet in a perfectly circular orbit.
See: http://i.imgur.com/mQ9yWUR.png
I have the planet orbiting fine, which is to say, transform.RotateAround-ing fine, but I was wondering how you should approach the problem of translating the secondary orbiter (the moon), while still applying the rotation logic to it. If you only rotate it, then it's not travelling with the planet, and will fly off into "space".
I know that RotateAround probably isn't the best approach to this, and I'm not really used to using helpers, so maybe it's better for me to just manually calculate the positions and set them, but this must be something everyone has done before so any ideas or advice you have would be greatly appreciated.
Just in case you were interested in the code snippet I'm using to orbit, here it is:
#pragma strict
public var _targetId : String = "";
public var _orbitSpeed : float = 10f;
private var _target : GameObject;
private var _rotationAxis : Vector3 = Vector3(0, 0, 1);
function Start () {
_target = GameObject.Find(_targetId);
}
function Update () {
transform.RotateAround(_target.transform.position, _rotationAxis, _orbitSpeed * Time.deltaTime);
}
Thanks!
Your answer
Follow this Question
Related Questions
Why isn't this code working? 1 Answer
What is the best way to orbit objects in a scientific-like elliptical orbit? 0 Answers
How to stop orbiting movement and keep going straight at converging speed 1 Answer
Maths question - normalization I think 1 Answer
Predicting the hit point on X axis, based on vector direction 1 Answer