Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Big_Friggin_Al · Oct 11, 2015 at 03:08 PM · camerascreenbounds

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Konomira · Oct 11, 2015 at 03:46 PM 0
Share

Have you tried using Camera.ViewportToWorldPoint?

docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html

avatar image Big_Friggin_Al Konomira · Oct 11, 2015 at 04:21 PM 0
Share

Yes, the problem is that function requires a 'distance' parameter, and the distance varies due to the camera rotation. Hence my problem.

avatar image Konomira Big_Friggin_Al · Oct 11, 2015 at 04:33 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }
     }
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Oct 11, 2015 at 06:54 PM 0
Share

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.

avatar image Big_Friggin_Al · Oct 12, 2015 at 05:39 AM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

How can I get the on-screen render bounds of a Unity 4.6 GUI component whose canvas is set to World Space? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges