Problem with ViewportToWorldpoint

ViewportToWorldpoint isn’t giving me the values I’d expect, I want to get the coordinates of the corners.

I’m using an orthographic camera for a 2D game and the following code.

 Vector3 bottomLeft = mainCam.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
 Vector3 topRight = mainCam.ViewportToWorldPoint(new Vector3(1f, 1f, 0f));

The values I’m getting are way off the screen, like more than twice the size of the camera window.

I already tried changing the Z-Coordinate, but that did nothing.

the documentation states that the z value should be your camera’s near clip plane.

Unity - Scripting API: Camera.ViewportToWorldPoint (unity3d.com)

As I stated, changing the z position changed nothing.
Probably since I’m using an orthographic camera in 2D space and not in 3D space as in the documentation.

i assumed you just tried some arbitrary number, but yeah i know some functions work differently with ortho camera.

have you tried using screentoworldpoint?

ScreenToWorldpoint is giving me the same values, except for the z pos, however since it’s 2D this doesnt make a difference.
I’ve tried ScreenToViewport too and it’s giving me (0,0,0 and (1,1,0) as expected, so the input values are at least correct.
But when I’m trying to convert either to Worldpoint it breaks.

The code below will create a quad and place it in the top right corner of the screen. In orthographic mode the quad will be very small.

    Vector3 topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, Camera.main.nearClipPlane));
    GameObject obj=GameObject.CreatePrimitive(PrimitiveType.Quad);
    obj.transform.position=topRight+Camera.main.transform.forward*0.02f; // Position the quad and push it forward slightly so it doesn't get clipped
    obj.transform.localScale=Vector3.one*0.1f;

I discovered the problem.
I’ve been using a 2nd camera to create a render texture, for some reason it was tagged as main camera too (I don’t know why since newly created cameras are per default untagged), after changing the tag everything worked.