# Rotating an GameObject in a 2D plane based on clicking on a game object.

Hi,

I have been trying to solve a problem for a while and I feel like I am going in circles, so I thought I would try here.

Firstly, I apologise for the crudeness of the image.

I have a Circle (Blue) with a Game Object in the center of it (Black Circle). The Camera (Green) is a child of the Black Circle.

I am trying to rotate the camera, to the randomly instantiated Game Objects (Red) When they are selected, by rotating the Black Circle, however, I am running into difficulties when the Black Circles rotation value goes into negative values.

I haven’t had great success with this so any help in pointing me in the right direction would be much appreciated.

Hi, so I managed to get something working very close to what I wanted, I think I can tune it in from here.

But I wanted to share my solution so here it is:

``````if (Input.GetMouseButtonDown(0))
{
// Getting Camera.
Camera myCamera = Camera.main;
// Get parent Transform from camera
Transform cameraRotator = myCamera.transform.parent.transform;

// Get positions of the building in question and the cameraRotator.
Vector3 currentPos = cameraRotator.position;
Vector3 targetDir = transform.position;

// Subtracting the Vector3's from each other.
Vector3 relativePos = targetDir - currentPos;

// Create the Quaternion we need to rotate by.
Quaternion rotationDelta = Quaternion.FromToRotation(cameraRotator.transform.up, relativePos);

// Nuke the Y and Z Axis back to zero on the delta
var angles = rotationDelta.eulerAngles;
angles.y = angles.z = 0f;
rotationDelta = Quaternion.Euler(angles);

// Multiply the values together.
Quaternion targetRotation = cameraRotator.rotation * rotationDelta;

// starting the routine that will allow us to lerp this aforementioned value.
StartCoroutine(Transition(cameraRotator, targetRotation));

}

IEnumerator Transition(Transform cameraRotator, Quaternion targetRotation)
{
float step = 0.0f;
while (step < 1.0f)
{
// The step size is equal to the speed times frame time.
step += Time.deltaTime * (Time.timeScale / transitionDuration);
cameraRotator.rotation = Quaternion.Lerp(cameraRotator.rotation, targetRotation, step);
yield return 0;
}
}
``````