Calculate position of camera with set distance and angle.

Despite having my last question answered >>CLICK<<, I still can’t wrap my head around it. I might have to rephrase my question:

I can adjust the angle between my camera and my character, by using the MouseX and MouseY axes.

When I have this angle, I want my camera to be x units away from the character, no matter the angle. How can I calculate a vector3 with this input?

Could you please also tell me how I represent this distance between the camera and the character (int, vector3,…)?

If X is the longitudinal angle (0 to 360) and Y is the latitudinal (-90 to +90), you could:

camera.transform.position = player.transform.position + Quaternion.Euler(Y,X,0);

You probably also want:


Also note that X should keep going past 0 and 360 when the user move the mouse further.

There is already a camera script in Standard Assets for this (MouseOrbit).

If you subtract the camera position from the player’s position, the result is a vector indicating the direction camera->player, and its magnitude will be the distance between their pivots (positions). Supposing cam and player are variables containing the camera and player transforms:

  var vCam:Vector3 = player.position - cam.position; // vector camera->player
  var dist:float = vCam.magnitude; // distance between camera and player

Or, if you want to get a position at dist distance from the player, and rotated V degrees in the vertical plane and H degrees in the horizontal plane, you can do:

  var vCam:Vector3 = Quaternion.Euler(V,H,0) * -Vector3.forward;
  cam.position = player.position - vCam * distance;

This rotates Vector3.forward V and H degrees as specified above, and place the camera at the distance dist in that direction.

NOTE: depending on the reference you have, you may find the V angle going in the opposite direction; if it happens, use -V instead. The same applies to the H angle - if you expect it to go clockwise, but it goes counter-clockwise, use -H.