Get screen bounds with **rotated** ortho camera??

This seems like it should be very simple but is proving very difficult…

I have a 3d scene, with an orthographic camera. It is rotated by 30 degrees (relative to the ‘gameplay plane’).

I want to instantiate objects at the edge of the screen, but I can’t find out what that would be!!!

I’ve tried using all of the usual answers people have given to similar issues, but none of them seem to take into account the camera’s rotation (which seems to screw up all of the usual methods of finding this out).

I can’t possibly be the only person who’s trying to do this with a rotated camera, so what am I missing here?

You mention that you’re using an orthographic camera. That simplifies the math considerably on determining the camera’s boundaries.

That said, I believe a better answer than I would be able to provide right now can be found here.

Once you’ve calculated the bounds of the camera, you can generally add the instantiated object’s bounding box extents to your camera’s transform.up and -.right to put it exactly off the edge of the screen.

If your game is 2½D (i.e, 3D view, 2D gameplay) and you want to instantiate objects at screen edges on a plane, cast a ray from the edge of the viewport onto a plane you define. Then you will be able to instantiate objects on that plane, at viewport edges.

Otherwise I don’t know what your distance that you talk about in your comments is. Distance to what?

Perspective camera

Orthographic camera

Here I cast a ray from screen edges on a plane at origo. I added the box to more clearly see where that plane would be. I am instantiating spheres here, ladies and gentlemen. It doesn’t get better than that. The camera is rotated along all three axes.

Was this effect something like what you were looking for? You might want to move the objects slightly off camera so they don’t pop into view the way they do here, but that is another sub problem to be solved on its own right.

using UnityEngine;

public class ScreenEdgeExample : MonoBehaviour
{
    public GameObject prefab;

    void Start()
    {
        ScreenInstantiate.LeftEdge(prefab);
        ScreenInstantiate.RightEdge(prefab);
        ScreenInstantiate.TopEdge(prefab);
        ScreenInstantiate.BottomEdge(prefab);
    }
}

public static class ViewportPoint
{
    public static readonly Vector2 Up = new Vector2(0.5f, 1.0f);
    public static readonly Vector2 Down = new Vector2(0.5f, 0.0f);
    public static readonly Vector2 Left = new Vector2(0.0f, 0.5f);
    public static readonly Vector2 Right = new Vector2(1.0f, 0.5f);
}

public static class ScreenInstantiate
{
    public static GameObject LeftEdge(GameObject original)
    {
        return AtViewportPoint(original, ViewportPoint.Left);
    }

    public static GameObject RightEdge(GameObject original)
    {
        return AtViewportPoint(original, ViewportPoint.Right);
    }

    public static GameObject TopEdge(GameObject original)
    {
        return AtViewportPoint(original, ViewportPoint.Up);
    }

    public static GameObject BottomEdge(GameObject original)
    {
        return AtViewportPoint(original, ViewportPoint.Down);
    }

    public static GameObject AtViewportPoint(GameObject original, Vector2 viewportPoint)
    {
        // Plane is facing back and at (0, 0, 0).
        // Depending on your game, you may want to change this (to ground for top-down)
        // Consider promoting plane to a parameter.
        Plane plane = new Plane(-Vector3.forward, Vector3.zero);
        Ray ray = Camera.main.ViewportPointToRay(viewportPoint);
        float distance;

        if (plane.Raycast(ray, out distance))
        {
            GameObject clone = Object.Instantiate(original);
            clone.transform.position = ray.GetPoint(distance);
            return clone;
        }
        else
        {
            // Fallback solution...
            Debug.LogWarning("Failed to Instantiate prefab at viewport point.

"+
“Defaulting to a normal Instantiate.”);
return Object.Instantiate(original);
}
}
}