Skip to content

Instantly share code, notes, and snippets.

@Streamweaver
Created January 18, 2020 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Streamweaver/a9c926d347e34b483eb324fa75163131 to your computer and use it in GitHub Desktop.
Save Streamweaver/a9c926d347e34b483eb324fa75163131 to your computer and use it in GitHub Desktop.
This is a small utility script I use in simple 2D Unity games to world coordinates. I'd probably make this static and just pass in a cam object in the future so I can call it anytime.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boundary : MonoBehaviour
{
Camera cam;
public float width, height;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
}
void FindBoundaries()
{
width = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0)).x - 0.5f);
height = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0)).y - 0.5f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment