# How to rotate the camera around a casted ray

I’m attempting to make a Real-Time Strategy game. The camera floats above the battle field. I’m trying to create the camera controls, so that when a user hits a button, the camera will rotate around whatever it is looking at.

I figured the first thing to do is cast a ray downwards (from the camera’s perspective), so that it hits the terrain.

The next part I’m a little fuzzy on. The camera position needs to move around the hit position, and the rotation needs to change, so that it is still looking at the same point.

Here is a picture:

Any help would be appreciated. Thanks.

This should do the job for your specific question:

``````Transform myTransform = camera.main.transform;
myTransform.RotateAround(raycastHit.point, Vector3.up, ROTATION_SPEED);
``````

Easier way to do the camera control would be to place empty GameObject on ground level, make it parent of your camera, and then rotate/move around that empty.

For those curious, here is what I got working:

``````	// rotate camera around what we're looking at
float rot = Input.GetAxis("Horizontal");
if (rot != 0)
{
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, ray.direction, out hit, 1000))
{
Debug.DrawLine(ray.origin, hit.point);
float dx = cam.camera.transform.position.x - hit.point.x;
float dz = cam.camera.transform.position.z - hit.point.z;
float ang = Mathf.Atan2(dz, dx);
float mag = Mathf.Sqrt(dx * dx + dz * dz);
ang += rot / 100;
dx = Mathf.Cos(ang) * mag;
dz = Mathf.Sin(ang) * mag;
cam.camera.transform.position = new Vector3(hit.point.x + dx, cam.camera.transform.position.y, hit.point.z + dz);
cam.camera.transform.LookAt(hit.point);
}
}
``````