0
\$\begingroup\$

This question is related to my other question about camera rotation. I hope its acceptable to ask a second question. I cannot figure out the math of how to calculate moving the camera to stay behind my player, based on the players rotation.

enter image description here

In the image above, the arrow represents a player with the direction of the arrow representing the direction the player is facing. The red object represents camera. I want the camera to always remain behind the player. When the player turns, I want the camera to move behind the player. I cannot figure out the math for calculating where to move to.

Step 1 The arrow is at 0,0 and facing 0 (north). The red circle is at 0,-5

Step 2 the arrow is at 0,0 and turned 90 degrees to the right (facing east). The red circle should move to be "behind the arrow" aka to the left and up and at the same distance from the arrow as when it started. I am guessing that would be -5,0 in this case

Other Scenarios a) The arrow is at 0,0 and turned -30 degrees (turned to the left). The red circle should move right and up.

b) the arrow is at 0,0 facing south (180 degrees opposite of #1) and turns -30 degrees to right (on screen but left if you are the arrow). The red circle should move appropriately.

I have racked my brain over this for days and just cannot find an answer. Thnx Matt

\$\endgroup\$
1
\$\begingroup\$

TL;DR

camera.position = player.position - player.forward * distanceFromPlayer;

player.position gives the position of the player

player.forward gives the direction the player is looking at. Obviously, when the player rotates, the vector rotates too. - player.forward will allow the camera to be placed behind the player.

distanceFromPlayer is a (positive) float whose value represents the distance between the player and the desired position

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I can't say thank you enough. I was going nuts trying to solve this! \$\endgroup\$ – tatmanblue Feb 12 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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