Check which part of object is front to the camera

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?

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.

1 Like

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.

Is your camera fixed then? You could just check which direction is towards the camera in that case.

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”.

Of course it can, why do you say that? Every form of “LookAt” rotation in Unity has an optional second parameter which is an “up” vector: https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

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.

using UnityEngine;

public class GetDirectionToCamera : MonoBehaviour
{
    public enum Direction
    {
        Up,
        Down,
        Forward,
        Back,
        Left,
        Right,
        Unknown
    }

    private Vector3[] _directions = new Vector3[]
    {
        Vector3.up,
        Vector3.down,
        Vector3.forward,
        Vector3.back,
        Vector3.left,
        Vector3.right
    };

    private Transform _cam, _t;

    private void Start()
    {
        _cam = Camera.main.transform;
        _t = transform;
    }

    private void Update()
    {
        Debug.Log(GetDirectionVector(_cam.position - _t.position));
    }

    private Direction GetDirectionVector(Vector3 direction)
    {
        var maxDot = -Mathf.Infinity;
        var ret = Direction.Unknown;

        for (var i = 0; i < _directions.Length; i++)
        {
            var dot = Vector3.Dot(direction, _t.rotation * _directions[i]);

            if (dot > maxDot)
            {
                ret = (Direction)i;
                maxDot = dot;
            }
        }

        return ret;
    }
}

Why do the sprites need to change though, is rotating them to face the camera not what you want to do?

1 Like

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

That was brilliant, thanks :slight_smile:
Got it working with

public enum Direction
    {
        LeftForward,
        Forward,
        RightForward,
        RightBack,
        Back,
        LeftBack,
        LeftForwardUp,
        ForwardUp,
        RightForwardUp,
        RightBackUp,
        BackUp,
        LeftBackUp,
        LeftForwardDown,
        ForwardDown,
        RightForwardDown,
        RightBackDown,
        BackDown,
        LeftBackDown,
        Unknown
    }

    private Vector3[] _directions = new Vector3[]
    {
        new Vector3(1,0,0.6f),
        Vector3.forward,
        new Vector3(-1,0,0.6f),
        new Vector3(-1,0,-0.6f),
        Vector3.back,
        new Vector3(1,0,-0.6f),
        new Vector3(1,1,0.6f),
        new Vector3(0,1,1),
        new Vector3(-1,1,0.6f),
        new Vector3(-1,1,-0.6f),
        new Vector3(0,1-1),
        new Vector3(1,1,-0.6f),
        new Vector3(1,-1,0.6f),
        new Vector3(0,-1,1),
        new Vector3(-1,-1,0.6f),
        new Vector3(-1,-1,-0.6f),
        new Vector3(0,-1-1),
        new Vector3(1,-1,-0.6f)
    };