# How to Replace two object by rotating in center

I created Find Rock game but I have problem In replacing two object by rotating in center and when rotating finish stop rotating how can I do this?

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public GameObject[] obj;
public int[] Randomize = new int[2];

void Start(){
StartCoroutine (Delay ());

}

IEnumerator Delay(){
Randomize [0] = Random.Range (0, obj.Length);
Randomize [1] = Random.Range (0, obj.Length);
while (Randomize [0] == Randomize [1]) {
Randomize [0] = Random.Range (0, obj.Length);
}
yield return new WaitForSeconds (2);
obj [Randomize [0]].transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 0));
obj [Randomize [1]].transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 0));

StartCoroutine (Delay ());
}
void Update(){
if (obj [Randomize [0]].transform.rotation.eulerAngles.y < 90 || obj [Randomize [1]].transform.rotation.eulerAngles.y < 90) {
obj [Randomize [0]].transform.RotateAround (obj [Randomize [1]].transform.position, Vector3.up, Time.deltaTime * 100);
obj [Randomize [1]].transform.RotateAround (obj [Randomize [0]].transform.position, Vector3.up, Time.deltaTime * 100);
}
}
}


I can make this game but when rotating finish they aren't Directed. this video link describe my problem and this is unity package of my game. how can I use mathf clamp RotateAround for stopping rotation?!

## 1 Answer

Because of the discrete nature of time, the last rotation step you do ends up being too much. You have two options: (1) detect when you are doing the very last rotation update and make sure it's not too much, or (2) when your rotation is over, set the position/rotation of the objects to where they should be. I will describe (1) below:

void Update(){
if (obj [Randomize [0]].transform.rotation.eulerAngles.y < 90 || obj [Randomize [1]].transform.rotation.eulerAngles.y < 90) {
float angleToRotate = Time.deltaTime * 100;
float angleRemaining = 90 - obj [Randomize [0]].transform.rotation.eulerAngles.y;
if(angleRemaining < angleToRotate) {
angleToRotate = angleRemaining;
}
obj [Randomize [0]].transform.RotateAround (obj [Randomize [1]].transform.position, Vector3.up, angleToRotate);
obj [Randomize [1]].transform.RotateAround (obj [Randomize [0]].transform.position, Vector3.up, angleToRotate);
}
}


This should prevent your boxes from moving too much and they should stay on a line.

• thanks but don't work! I don't know why I can't stop it on time in direct position – Seyed Morteza Kamali Aug 2 '16 at 12:42
• it should work. did nothing change after you made this edit? you can't stop it on time, because there is no frame at that time. your "time" is between two frames. therefore you have to correct the overshoot. – Gazihan Alankus Aug 2 '16 at 13:11