Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm making a camera that can currently rotate freely from the back to the front of a target object by giving it an angle and a distance.

enter image description here

I do the above with the following code

directionVector = new Vector3(0, Mathf.Sin (angle*Mathf.Deg2Rad), Mathf.Cos(Mathf.Deg2Rad * angle));
normalizedDirectionVector = (directionVector).normalized;

transform.position = (normalizedDirectionVector*cameraDistance) + target.transform.position;

In the above I take the direction from the angle, normalize it and then add it to the target position. And the camera sets nicely to the wanted angle. But how can I add the sideways movement(x-component) so that I could move the camera freely around the target?

enter image description here

share|improve this question

marked as duplicate by bummzack, Anko, doppelgreener, Sean Middleditch, Byte56 Oct 12 '13 at 3:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

The manpage for gluLookAt might be of use. That is deprecated now days in OpenGL itself, but glm has an implementation. Then you just use that plus basic trigonometry for working out the X/Y coordinates of the camera.

share|improve this answer

You could use engine features to avoid trigonometry entirely. Another way to achieve this is to do the following steps:

  1. Set the camera's position equal to the target position.
  2. Rotate the camera such that it points in the direction you want.
  3. Add the desired displacement to the local translation of the camera. This will automatically move the camera away from the target. (Note: depending on the axes you are using, this might mean adding <0, 0, orbitRadius> or <0, 0, -orbitRadius>.)

It looks like you're using Unity, which has the tools to make this extremely simple.

share|improve this answer

Google Euler angle conversion for more info. To get a direction vector from a pitch and a yaw (pitch is what you have now and yaw is around the vertical axis) you want this:

x = cos(yaw)*cos(pitch)
y = sin(yaw)*cos(pitch)
z = sin(pitch)
share|improve this answer
I tried this approach it worked fantastically when e.g editing yaw with pitch as zero. But when both had values the rotation became limited. – Esa Oct 11 '13 at 5:28
up vote 0 down vote accepted

I solved this by using the original angle vector and then using the equation that @nykwil provided, but only using the pitch component. Then I combined the y-component of the angle vector to the pitch and I got the result I wanted.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.