- Home /
How to reliably orbit a position with joints or any other way
I simply want a game object to orbit a fixed point on a plane. Imagine a standard model from school of the earth orbiting the sun.
What makes it complicated is that I also want my game object to still react to other objects, for example if it is hit from the front by a bouncy object it should change direction while still keeping its distance to the center point.
Before Unity, I build the exact same thing with Box2D (different Physics engine). I simply fixed a joint to the center point and the game object. Done.
In Unity, it does not seem to be that easy. The moment my game Object gets fast or is hit by another object, the joint will not stay in place. It will wiggle and the circular motion might even become more of a rectangle if movement is fast. I tried a hinge joint with the hinge on top of the center Point and also linking a central object to the circling one via fixed joints and a long object in between.
I have learned that the problem can be helped by a) lowering the minimum time step b) raising the iteration count
I tried both options and they wont do for me, since keeping the distance in that case is really central to my game and a) both options still won't fix the problem completely b) I am afraid that I will regain rectangular motion when fps drop, as the time step will then go up.
What I am now planning to do is to use Transform.RotateAround, thereby orbiting by hand. This means that i will have to handle any physics related Behavior myself, since Transform.RotateAround is not part of the physics engine, which means it does not cause any real movement.
That sucks.
So I would like to know: Is there any other way to orbit a position? Or maybe even a way to make a joint stay in place, therefore reliably keeping a distance between an object an a point in space?
Answer by robertbu · Jan 11, 2013 at 07:34 PM
Try:
Add a configurable joint component
Freeze all motion and angular motion settings except the axis you want to rotate around (Y in my test case).
Move the object to the "track" and then positions the anchor at the center.
Turn on projection mode for position and rotation
Set Projection Angle to 0.01
In my simple test, I had minimal wobble with a solver iteration rate at the default of 6.
Given that you have a predictable track, you could also move the object with Rigidbody.MovePosition() and you could limit the velocity so mute the effects of large impacts. Changing the mass also can help. I gave the ball a mass of 0.1f in my test.
Thanks, but as you have stated, none of these $$anonymous$$ethods gets rid of the wobbling completely. It might be tolerable, but I guess I am just to paranoid to accept such a less than exact simulation.
Especially since it might hurt me in the long run, because the movement might become extremely fast and the radius extremely small, which will lead to bad behavior however small the wobbling is in normal cases.
Also, I can not (read : do not want to) change the mass, as it would affect the simulation in every other way as well.
Rigidbody.$$anonymous$$ovePosition is no option either. When you use it, the velocity of your body is zero as far as the physics engine is concerned. That means every other Object will act very strange when it hits you, as you are clearly visibly moving while the collision works as if you are standing still.
What I ended up doing for now is similar though: First I rotate where I want to go within the next fixedUpdate with transform.RotateAround. I remember that place and rotate back via transform.RotateAround as well. Then I set up my velocity so that I will reach that point at the end of the next fixedUpdate. That way I get to have a real velocity and it is exact enough that it probably takes thousands of orbits until I get to a place I do not want to go due to rounding errors.
If anybody knows a way to actually fix Objects together in a 100% reliable way I'd still be interested, but by now I doubt it :-)
$$anonymous$$arked this as an answer, as the final answer seems to be "not possible".