Camera Following Moving/Rotating Object

Hey guys!

I have a bit of a problem. I have a sphere that is rotating (think of a marble) and an empty object has it’s position constantly updated to match the position of the sphere. The empty object rotates with the iPhone’s accelerometer and the ball takes off in the direction that the empty object is facing. So far so good! Now, the problem is getting the camera to follow the ball. It is easy enough to get the camera to follow the ball left/right, up/down and backward/forward, but as far as being behind the sphere’s local z axis, nothing I do works.

To illustrate the problem, I’ve attached an embarrassingly crude image. On the left is the camera (rectangle) sliding over when the ball goes off to one side (BAD). On the right is the camera getting behind the sphere’s local z axis (this is where you guys come in) and looking at the sphere (LookAt(transform).

Now, I tried using the code below to make the camera stick behind the sphere’s z axis (actually, its the empty object’s z axis because the ball is spinning), but it does not work.

Camera.main.transform.localPosition.z=transform.localPosition.z-7.0;

I also tried taking the local part out of the camera end of the deal, but it didn’t work. Any thoughts?

Thanks :slight_smile:

443986--15431--$sample.png

Sorry to bump the thread, but I know that this is very possible (and probably relatively easy). I’ve spent a lot of time on this and, for the life of me, I can’t figure it out.

If the first post does not make sense to you, let me know and I’ll try to explain it better :stuck_out_tongue:

EDIT: I got it to work! I simply parented the camera to the empty object (that is on the sphere) and put some code into the start function and wa la! I thought that I had tried it before, but since my code is ever-changing (as well as just about everything else in my project), it could have been that something that I had setup prevented the camera from following the sphere. Who knows.