Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active November 23, 2019 20:19
Show Gist options
  • Save SiarheiPilat/4b6acc0fd771c8960d2fde337ee428a9 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/4b6acc0fd771c8960d2fde337ee428a9 to your computer and use it in GitHub Desktop.
using UnityEngine;
/// <summary>
/// The following script converts camera viewport corners' coordinates from viewport to world space.
/// </summary>
public class ScreenCornersToWorld : MonoBehaviour
{
public Camera Camera;
public Vector3 BottomLeftCorner, TopLeftCorner, TopRightCorner, BottomRightCorner;
void Start()
{
if (!Camera) Camera = Camera.main;
BottomLeftCorner = Camera.ViewportToWorldPoint(new Vector3(0, 0, Camera.nearClipPlane));
TopLeftCorner = Camera.ViewportToWorldPoint(new Vector3(0, 1, Camera.nearClipPlane));
TopRightCorner = Camera.ViewportToWorldPoint(new Vector3(1, 1, Camera.nearClipPlane));
BottomRightCorner = Camera.ViewportToWorldPoint(new Vector3(1, 0, Camera.nearClipPlane));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment