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. Dismiss Notice

Check which part of object is front to the camera

Discussion in 'Scripting' started by Wojzax, May 17, 2020.

  1. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    So I need to check which part of sphere object is front to the camera. I need to check 18 areas, 6 on y axis, and 3 on x axis.
    On y axis I just check Object.transform.eulerAngles.y, and get 0-360 angle which I can easily change to one of the areas.
    But on x axis, when I check Object.transform.eulerAngles.x, I get four quaters (0-90, 90-0, 270-360, 360-270). I can't really get anything from this variable. How can I check this?

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Do you have the code to generate center points of these areas on the surface of the sphere? If so, you can iterate through the points and check which one is closest to the camera. For a tiny bit of extra performance, compare Vector3.sqrMagnitude instead of Vector3.magnitude.
     
    Last edited: May 17, 2020
    khaled24 likes this.
  3. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    I could make an Vector3 array with those points, check them every frame and then iterating trough them check which one is closest to the camera. But I need many of those objects in scene and each one of them would have billboard with a sprite changing with the rotation.
    That would be too much I think, and thats why I wanted to check it with rotation.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Is your camera fixed then? You could just check which direction is towards the camera in that case.
     
  5. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    I was trying to get the direction first with the camera staying in one place, but had no luck in it.
    My billboards are working as intented, but i need to change the sprite on them according to the direction. Problem is Unity can't distinguish between rotations when object is "normal" and "upside-down".
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    There are a number of ways of doing it. You could always just compare the dot product of the direction to the camera and the directions of the object.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GetDirectionToCamera : MonoBehaviour
    4. {
    5.     public enum Direction
    6.     {
    7.         Up,
    8.         Down,
    9.         Forward,
    10.         Back,
    11.         Left,
    12.         Right,
    13.         Unknown
    14.     }
    15.  
    16.     private Vector3[] _directions = new Vector3[]
    17.     {
    18.         Vector3.up,
    19.         Vector3.down,
    20.         Vector3.forward,
    21.         Vector3.back,
    22.         Vector3.left,
    23.         Vector3.right
    24.     };
    25.  
    26.     private Transform _cam, _t;
    27.  
    28.     private void Start()
    29.     {
    30.         _cam = Camera.main.transform;
    31.         _t = transform;
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         Debug.Log(GetDirectionVector(_cam.position - _t.position));
    37.     }
    38.  
    39.     private Direction GetDirectionVector(Vector3 direction)
    40.     {
    41.         var maxDot = -Mathf.Infinity;
    42.         var ret = Direction.Unknown;
    43.  
    44.         for (var i = 0; i < _directions.Length; i++)
    45.         {
    46.             var dot = Vector3.Dot(direction, _t.rotation * _directions[i]);
    47.  
    48.             if (dot > maxDot)
    49.             {
    50.                 ret = (Direction)i;
    51.                 maxDot = dot;
    52.             }
    53.         }
    54.  
    55.         return ret;
    56.     }
    57. }

    Why do the sprites need to change though, is rotating them to face the camera not what you want to do?
     
    Wojzax likes this.
  8. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    Right, but that's Quaternions and i can't just break them into three simple axis'es, and I think I need that to check range on two axis'es
     
  9. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    That was brilliant, thanks :)
    Got it working with

    Code (CSharp):
    1. public enum Direction
    2.     {
    3.         LeftForward,
    4.         Forward,
    5.         RightForward,
    6.         RightBack,
    7.         Back,
    8.         LeftBack,
    9.         LeftForwardUp,
    10.         ForwardUp,
    11.         RightForwardUp,
    12.         RightBackUp,
    13.         BackUp,
    14.         LeftBackUp,
    15.         LeftForwardDown,
    16.         ForwardDown,
    17.         RightForwardDown,
    18.         RightBackDown,
    19.         BackDown,
    20.         LeftBackDown,
    21.         Unknown
    22.     }
    23.  
    24.     private Vector3[] _directions = new Vector3[]
    25.     {
    26.         new Vector3(1,0,0.6f),
    27.         Vector3.forward,
    28.         new Vector3(-1,0,0.6f),
    29.         new Vector3(-1,0,-0.6f),
    30.         Vector3.back,
    31.         new Vector3(1,0,-0.6f),
    32.         new Vector3(1,1,0.6f),
    33.         new Vector3(0,1,1),
    34.         new Vector3(-1,1,0.6f),
    35.         new Vector3(-1,1,-0.6f),
    36.         new Vector3(0,1-1),
    37.         new Vector3(1,1,-0.6f),
    38.         new Vector3(1,-1,0.6f),
    39.         new Vector3(0,-1,1),
    40.         new Vector3(-1,-1,0.6f),
    41.         new Vector3(-1,-1,-0.6f),
    42.         new Vector3(0,-1-1),
    43.         new Vector3(1,-1,-0.6f)
    44.     };
    45.