- Home /
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?
Have you tried using Camera.ViewportToWorldPoint?
docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html
Yes, the problem is that function requires a 'distance' parameter, and the distance varies due to the camera rotation. Hence my problem.
Does your camera move at all? Seeing as the camera is orthographic, the edges of the screen will have the same x co-ordinate regardless of the z. If your camera doesn't move it might just be best to figure out the co-ordinates through trial and error.
Answer by Statement · Oct 11, 2015 at 06:44 PM
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.\n"+
"Defaulting to a normal Instantiate.");
return Object.Instantiate(original);
}
}
}
And of course you can change the method of raycasting onto a plane to ins$$anonymous$$d use Phsyics.Raycast to put items on terrain etc.
Thank you!
Your solution is exactly what I was trying to figure out. I adapted your ideas with my own code and it works perfectly in my project now.
Before you'd responded I actually got as far as setting up a Plane that matched my gameplay plane, and started to research how to best get distances based on that, but that's as far as I got.
Good to know I was at least on the right track, but you definitely moved my understanding forward by a good number of research hours!
Answer by Eno-Khaon · Oct 11, 2015 at 06:23 PM
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.
http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html
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.
Your answer
Follow this Question
Related Questions
Why doesn't my players camera work and why does it spaz out randomly 1 Answer
why my unity games run on android devieces,the screen will be wrong. 0 Answers
drag a UI panel keeping it with the screen bounds 2 Answers
Keeping screen width and scaling screen height for different screen resolutions 2 Answers