# How to rotate the camera to the gameobject position.

I have a simple sphere Gameobject at position (0, 0, 0). My camera has the beginning rotation (0, 0, 0). Now i have set the inputs MouseX and MouseY to get the x, y and change the position of the camera.

``````if (mouseDown){
x = Input.GetAxis("MouseX");
y = Input.GetAxis("MouseY");
}
``````

That works all fine, but now i want to set the camera rotation to the center of the Sphere (0, 0, 0). So that I can rotate around (with holding mouse0) the sphere. I tried it with

``````  camera.transform.rotation.SetFromToRotation(camera.transform.position, target.transform.position);
``````

but it don’t work.

I’m sure that it easy but i don’t find the solution. I’m absolutely new in Unity.
Thanks.

The simplest solution is to create a pivot object for the camera (as the parent of the camera), and move/rotate that object instead of the camera itself.

Now, the Camera has the right rotation. I realized that with camera.transform.LookAt(astroid.position);, but now i have a other problem. The position of the camera is not perfect set and the camera don’t stop to rotate around the astroid if i start to drag the mouse.

That I use for the position:

``````if (mouseDown){
x += Input.GetAxis("MouseX");
y += Input.GetAxis("MouseY");
}

SetCameraRotation();
SetCameraPosition(rotation, x, y);
}

void SetCameraRotation(){
camera.transform.LookAt(astroid.position);
}

void SetCameraPosition(Quaternion rotation, float x, float y){
Quaternion unit = Quaternion.Slerp(camera.transform.rotation, astroid.position, Time.deltaTime * 3);
Vector3 position = unit * new Vector3(x * 0.1f, y * 0.1f, 0.0f) + camera.transform.position;
camera.transform.position = position;
}
``````

I hope someone can help me.

My understanding is that you have an object at the origin (asteroid), and you want a camera “above” the asteroid looking “down” (towards the origin). And you want to move the mouse to rotate the camera round the asteroid as if you are skimming along the surface looking down.

To do this, I would 1) place an empty game object at the center of rotation, 2) place the camera the right distance looking at the asteroid, 3) make the camera a child of the empty game object, and 4) attach a script to the empty game object that rotates the empty game object. The camera will be taken along for the ride and remain a fixed distance above empty game object. Here is simple few lines of code based on your GetAxis() above to rotate the empty game object:

``````void Update () {
if (Input.GetMouseButton(0)){
Vector3 v3T = new Vector3(Input.GetAxis("Vertical"),-Input.GetAxis("Horizontal"),0.0f);
transform.Rotate (v3T *0.1f);
}
``````