# Move around object

I have problem with Unity - 2D objects. I want to rotate object around circle and on click translate it (with same angle) outside the circle to another and still rotate.

Rotating is working with RotateAround. But what about translating it to another circle?

I explained it in the image: • you can lock object in center of the circle by joint then when you click change joint: before click object is attach circle 1 by joint after click object will attach circle 2 by new joint and disable first joint – Seyed Morteza Kamali Jul 17 '16 at 14:34

## 2 Answers

Here is one theoretical solution you can try.

Your moving object (that I'll call Dot) is moving around other objects (that I'll call Gravity Points). The Dot knows what Gravity Point it is attached to and should know the position of other Gravity Points. Also as they are defined as circles I imagine that they have a radius that you can easily know.

When the player clicks you can detach the Dot from its current Gravity Point and make it "float through space" until it enters in the radius of another one. To know the direction where to float to you just need to know the position of the current Gravity Point and the Dot position.

(Dot.position - GP.position).Normalize will give you the floating direction. Applying this direction every frame to the Dot will make it go straight through space.

Once done you need to know if the Dot meets a new Gravity Point. As every Gravity Point has a radius you just have to compute the distance from the Dot to all the Gravity Points and compare it to their radius. Doing something like Vector3.Distance(Dot.position,GP.position) will give you the distance from the Dot to a given Gravity Point. If this distance is shorter or equal to the Gravity Point radius, then you can attach your dot to it, and start the rotation again.

I hope it helps.

A quick and easy solution if you do not want to deal with too many calculations (you can refer to Ivictorino's solution if you want that), is to simply parent the small rotating circle to the big circles.

The hierarchy might look something like this:

BigCircle
|
|_ EmptyObjectAtCenterOfBigCircle
|
|_ SmallRotatingCircle


In code, all you're doing is rotating the "EmptyObjectAtCenterOfBigCircle". When you need to do the transfer, lerp the small circle to the appropriate position, reset the parent, and start spinning the new "EmptyObjectAtCenterOfBigCircle".

smallCircle.transform.position  = Vector3.Lerp(
smallCircle.transform.position,
targetCircle.transform.position + targetCircle.radius + 0.1F,
Time.deltatime);

//When the lerp is done:
smallCircle.transform.parent =
targetCircle.transform.Find("EmptyObjectAtCenterOfBigCircle");

targetCircle.GetComponentInChildren<SpinCode>().SetActive(true);
smallCircle.GetComponentInChildren<SpinCode>().SetActive(false);


Spincode simply rotates the EmptyObjectAtCenterOfBigCircle.