Created
January 18, 2020 04:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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