1
\$\begingroup\$

I have a player object who can rotate(face using mouse) up, down left right and can face straight. I am willing to know that how to determine that where is camera facing? Either it is looking up or down?? while the player is continuously moving in a up/down surface.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Use dot products.

Assuming left, down, up, and right are global directions you dot the Camera fwd with each vector respectively (You actually only need to do two calculation because of how dot products work).

It would look something like this

//Will return between 1 and -1. 1 means left, -1 means right
float leftRight = Vector3.Dot(camFwd,Vector3.left);
//Will return between 1 and -1. 1 means up, -1 means down, 
float upDown = Vector3.Dot(camFwd,Vector3.up);

Do whatever conditional checks you need to determine direction priority.

\$\endgroup\$
3
  • \$\begingroup\$ did you test this code? left is not exists \$\endgroup\$ Apr 13, 2017 at 15:42
  • \$\begingroup\$ Sorry, I made a typo, The 'l' in left should not have been capitalized. \$\endgroup\$
    – Zebraman
    Apr 13, 2017 at 15:51
  • 2
    \$\begingroup\$ Note that as written, you can get these numbers with less typing by upDown = cameraTransform.forward.y and leftRight = cameraTransform.forward.x (just note that this uses the opposite sign convention, with rightward directions positive) \$\endgroup\$
    – DMGregory
    Aug 9, 2017 at 11:58
0
\$\begingroup\$

Have the game check the camera's rotation. If the camera's x rotation is around 90 degrees they are facing up. if the camera's x rotation is around -90 degrees they are facing down. if the camera's rotation x rotation is around 0 they are facing forward and if it is around 180 they are facing backwards. Then have the camera spawn a ray that is equal to the rotation of the camera and use mathf.aproxemetly to see which rotation is closest.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .