# How to make an object rotate around the camera on Y axis

I have an object, cube which has the following lines of code:

``````Vector3 toPos = target.position + (target.rotation * defaultDistance);
toPos = new Vector3 (toPos.x, 0f ,toPos.z); //problem
this.transform.LookAt( target ) ;
Vector3 curPos = Vector3.Lerp (myTransform.position, toPos, distanceDamp * Time.deltaTime);
myTransform.position = curPos;
``````

The problem is: When I rotate the camera around the X-Axis, after some degrees (over 20), the cube object moves forward (into the camera’s position) instead of continue on moving in a circular movement.

What I’m trying to achieve, simply is like a solar system that the cube follows the camera’s rotation only on y axis.

Update #2:

`````` Vector3 toPos = target.position + target.rotation * new Vector3(defaultDistance.x, 0.0f, defaultDistance.z);
float multiplier = Mathf.Sqrt(defaultDistance.x * defaultDistance.x + (defaultDistance.z) * (defaultDistance.z));
//multiplier /= Mathf.Sqrt(toPos.x * toPos.x + toPos.z * toPos.z);
toPos.x *= -multiplier;
//toPos.y = target.position.y + defaultDistance.y;
toPos.z *= -multiplier ;
toPos.z -= 70;

Vector3 curPos = Vector3.Lerp(myT.position, toPos, distanceDamp * Time.deltaTime);
myT.position = curPos;
``````

I almost got it right, the cube still isn’t centered on the center. Can anyone help me figure out how to fix that?

Ok using your new code I see most of what you are talking about (although in my case the cube jumps to BFE instead of the camera location).

For simplicity’s sake I am focusing on getting the rotations right. Try doing the LookAt towards a vector3 representation of the camera but on the xz plane like this:

``````public Transform cameraT;
public Transform myT;

void LateUpdate () {
Vector3 clampedTargetPos = new Vector3 (cameraT.position.x, myT.position.y, cameraT.position.z);
transform.LookAt(clampedTargetPos);
}
``````

I hope that gives you the clues you need.

I think you are very close! I’ve mocked this up and got it working (as I understand it) and I made a video which shows what I did, but I’ll try to briefly explain here and if it doesn’t make sense then go check out the video and ask more questions here.

It looks like you are using the approach that I first used for rotating around something (adjust the angle and then move over a bit then rinse and repeat) however I think you will enjoy knowing about the API called Transform.RotateAround() which I think is really what you are looking for.

Heres the code I used to mock this up in my video:

``````public class AYRCubeOrbit : MonoBehaviour {

public Transform t;
public Transform ObjectToOrbit;
public Vector3 RotationDirections;
public float speed;

void Update() {
t.RotateAround (ObjectToOrbit.position, RotationDirections, speed * Time.deltaTime);
}
}
``````

Using that code I was able to just put the following into the inspector:

1. The orbiting objects transform and
2. The transform of the object to orbit around and then
3. Give it a Rotation Direction around the X Axis.

Heres the video so you can see what this does. I hope it helps but remember just let me know if it doesn’t and we will figure it right out!