Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting the main camera's direction?

Discussion in 'Scripting' started by Endzone, Apr 7, 2016.

  1. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hello, im attempting to make a script so that i can determin what direction the camera is facing but since its a vector is their a way i can transfer that into a bool ? so if the main camera is facing right for example how can i tell ?
    - Thanks
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You can use Vector3.Dot to compare how much two vectors align. If both are unit vectors the resulting value will be within the -1 to 1 range. 1 if they point exactly the same, -1 if opposite and 0 for perpendicular. So if the dot is > 0.9 they are pretty much aligned with some margin for error.
    Example:
    Code (csharp):
    1. if (Vector3.Dot(Camera.main.transform.forward, Vector3.right) > 0.5) Debug.Log("Facing right/east");
     
    Last edited: Apr 7, 2016
    whkona likes this.
  3. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Ohh i see awesome thanks for the help! :)
     
  4. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hey ive ran into a small problem and i have no clue how to fix it was wondering if you knew what it is im doing wrong ?
    i have added and altered your example code so i can get an output for all sides "left, right, forward, back" but i have ran into a small problem, for some reason their is like a void area where i get no output at all when i make the transition from facing forward to facing down/up? this is my code so far, and ive also made a quick snippit of the problem im running into Untitled-2.jpg
    Code (CSharp):
    1.         if (Vector3.Dot (Camera.main.transform.forward, Vector3.forward) > 0.9) {
    2.         //    Debug.Log ("Facing Foward/north");
    3.                 p = 2;
    4.         }
    5.             if (Vector3.Dot (Camera.main.transform.up, Vector3.forward) > 0.9) {
    6.                 p = 2;
    7.             }
    8.  
    9.         if (Vector3.Dot (Camera.main.transform.forward, Vector3.back) > 0.9) {
    10.         //    Debug.Log ("Facing back/south");
    11.                 p = 0;
    12.         }
    13.             if (Vector3.Dot (Camera.main.transform.up, Vector3.back) > 0.9) {
    14.                 p = 0;
    15.             }
    16.  
    17.         if (Vector3.Dot (Camera.main.transform.forward, Vector3.right) > 0.9) {
    18.         //        Debug.Log ("Facing right/east");
    19.                 p = 1;
    20.         }
    21.             if (Vector3.Dot (Camera.main.transform.up, Vector3.right) > 0.9) {
    22.                 p = 1;
    23.             }
    24.  
    25.         if (Vector3.Dot (Camera.main.transform.forward, Vector3.left) > 0.9) {
    26.         //        Debug.Log ("Facing Left/west straight");
    27.                 p = 3;
    28.         }
    29.             if (Vector3.Dot (Camera.main.transform.up, Vector3.left) > 0.9) {
    30.                 p = 3;
    31.             }
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    It is not quite suitable for this.
    With your needed result I'd probably go with something more like this:
    (Partial, typing on phone)
    Code (csharp):
    1.  
    2. Vector3 dir = Camera.main.transform.forward;
    3. float absX = Mathf.Abs(dir.x);
    4. float absY = Mathf.Abs(dir.y);
    5. float absZ = Mathf.Abs(dir.z);
    6. if(absX > absY && absX > absZ)
    7. {
    8.     if (absX > 0f) return Right;
    9.     else return Left;
    10. }
    11. else if ()  // repeat for y and z
    12.  
     
    pilabapoche likes this.