- Home /
Rotate multiple objects around a object, maintaining their own trajectory(not rotating their local forward vector)
I got a problem with rotations, the thing i am going for is basically something that looks like a atom. You know, one of these things:
![alt text][1] [1]: http://3.bp.blogspot.com/_ELH4mTZpwoY/S7RgJYr3yaI/AAAAAAAAAEM/-Hs6HmiIObE/s320/atom+(1).gif
I need units to spin around a center object, and i need to be able to shoot as many units as i want to that center object, and those units than need to spin around it, based on the trajectory they had when the came into the gravitational pull of that center object basically.
I already have something working that does just about that, using something as :
var lookDirection : Vector3 = target.position - transform.position;
var rotation : Quaternion = Quaternion.LookRotation(lookDirection);
transform.rotation = Quaternion.Lerp(transform.rotation,rotation,randomLookspeed;
transform.Translate(Vector3.forward * randomSpeed);
The problem with that, is that after a while all the units will be circling around that transform at the same height because each unit is lerped towards that. It will than look like a giant disc made up of units, or a galaxy like structure (cool, but not what i want), i want that atom effect.
Because the unit is always moving forward, rotating the local x axis should be enough to make it rotate around the center object while it is maintaining its own trajectory, but for some reason i can not get it done properly.
How can i set the Quaternion rotation so that visually it looks like the unit only rotates around the center object on its X axis?
Does the nucleus move? Are the 'electrons' spheres, or are they objects that must be rotated to face a certain direction with respect to their movement? What factor do you want to define how high an electron orbits? Are the electrons fired at the central object (you use the word shoot)? Is there criteria concerning how close they must pass in order to be captured? What should happen if the electron's trajectory is right at (i.e. it will hit) the nucleus? What defines the speed of the rotation, or do all the electrons rotate at the same speed?
And I don't understand the last two paragraphs. I'd handle this problem a differently than you have here, but I need to understand the design before I can give you an answer.
The nucleus can indeed move, but this should not be a problem if the units constantly lerp around the nucleus, perhaps the electrons move a bit slower sometimes than the nucleus is moving and need some time to catch up, but that is ok.
How high a electron should orbit should depend on the move speed of that electron, i want a fixed rotation speed for all units, but not a fixed move speed for all units. That means that slower moving electrons make smaller rotations around the nucleus than faster ones. This is already working as intended using the current code (the code snippet of my first post says "randomLookSpeed", this is wrong because it is a constant).
And they are not really shot, they are transferred from nucleus to nucleus but they approach their new nucleus with their move speed, and ram straight into the new nucleus when they first meet, than because the electrons touched that nucleus it is now their nucleus to orbit. From that moment, i would like the electrons to keep moving forward, but rotate around the nucleus as if they where trapped in a gravitational pull.
The current code i use, as posted in the first post does exactly that. Only it forces all units to rotate in a large disk like structure after a while.
I am aware that this sometimes means a electron can "crash" trough the nucleus a couple of times before it ends up in a nice smooth orbit, but this is not a problem.
That disk structure i would like to get rid of, i want the electrons to maintain their orbits around the nucleus as in the first couple of seconds rotating around it using my current code before it goes flat. It goes flat because it is forced to look at the nucleus over time, i understand this will result in a smooth orbit at one point at always the same height because the electrons are also always moving forward constantly. But i only want the x axis to look at that nucleus so not all electrons will end up in the same orbit at one moment. I think there is a really simple change that i need to implement in my current code so that it will not flat out at one point, but i fail to see it.
Answer by xandermacleod · Jul 23, 2013 at 08:07 AM
your best bet is to use constraints. for each electron create an empty gameobject (parented to nucleus) where it should be located, then constrain the electron to the empty gameobject.
this should keep the electron's forward vector the same as the nucleus rotates. if after that you want to spin the electron you may want to write a script that updates its rotation as the nucleus spins. you could even use a lookat script, although to lock off one axis you may need to alter it slightly, so that it (for example) looks at the nucleus position in X and Z but its own y position.
hope this helps.
Your answer
Follow this Question
Related Questions
Use Lerp to rotate object 2 Answers
Basic AI Locked Axis 1 Answer
How can I smooth the rotation using quaternion and raycast? 1 Answer
LookRotation return weird values 1 Answer