I’m creating an n*n*n Rubiks cube solver in unity.

I am rotating and moving each piece(cubie) individually instead of grouping and ungrouping (I think it should be more efficient).

I can’t get the rotation and movement of the cubies to line up when a side is turned:

**Here is the function used to produce the result above. It is called for every cubie on the rotating layer per frame:**

```
void RotateAndMove(GameObject obj, Vector3 targetPos, Quaternion targetRot, Vector3 rotationPoint, float speed)
{
var pos = obj.transform.localPosition;
var rot = obj.transform.localRotation;
//if the rotation is almost equal to the target rotation set both the rotatation and position to the target
if (EqualAprox(rot.x, targetRot.x) && EqualAprox(rot.y, targetRot.y) && EqualAprox(rot.z, targetRot.z) && EqualAprox(rot.w, targetRot.w))
{
obj.transform.SetLocalPositionAndRotation(targetPos, targetRot);
return;
}
var Amount = Time.deltaTime * speed;
//distance to center (radius)
var dist = Vector3.Distance(pos, rotationPoint);
//a point on the straight line between pos and targetpos
var newPoint = Vector3.MoveTowards(pos, targetPos, Amount * dist * 0.0195f);
//the new calculated position
pos = (newPoint - rotationPoint).normalized * dist + rotationPoint;
//the new calculated rotation
rot = Quaternion.RotateTowards(rot, targetRot, Amount);
obj.transform.SetLocalPositionAndRotation(pos, rot);
}
```

The MoveTowards function speed is slowed down by a number found through trial and error to try and keep the rotation and movement speed the same relative to each other but it seems like no number used here will align them precisely.

I think there is an issue with the formula and something else should be used instead but I have no clue how to fix it. I’m new to vector math.

Please suggest an algorithm or piece of code to rotate and move an object around a central point by a specified distance towards another point.

The unity rotate around function does almost what I need but it overshoots the target position.

Here is a similar question using lerp that almost works for me but it follows a straight line instead of rotating around: