Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JayC · May 26, 2013 at 03:10 PM · camerabounds

Find camera bounds at certain distance from camera?

Hey everyone. I am currently in the process of creating a 2.5D game that will use a camera similar to classic NES games. The most important constraint for this camera is that it remain within the bounds of the level - think a MegaMan style camera. To accomplish this, I am going to try and place colliders around the bounds of my levels and then place colliders around the edge of the camera's viewport. When a collision occurs, I know that the camera can not move in that direction anymore. I'm not sure if this implementation will work, but I'm trying to give it a shot. With that said, I need to place colliders at the edges of the camera's viewport at the z-position of the player. I am having trouble determining these positions.

I want to make this as robust as possible so that I don't run into issues if I zoom in/out. Additionally, I would like the camera to work on all aspect ratios. I could, of course, manually place these GameObjects to be at the camera bounds and it would work, but that's too specific.

My first instinct was to make this a triangle problem. I know the distance of the camera to the player and I know the angle of the camera's field of view. So, if my trig isn't wrong, I could use (tan(theta) dist) to find the vertical distance and then (tan(theta aspectRatio) * dist), where aspectRatio = Screen.width / Screen.height, to get the horizontal distance. Camera.fieldOfView gives the angle of the vertical, the horizontal is affected by the aspect ratio so that's why I'm multiplying the angle for the horizontal by aspectRatio. This was my first whack at the problem and it didn't work.

Does anyone have any suggestions?

TL;DR Find view port bounds at a certain distance away from the camera.

Comment
Add comment
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

1 Reply

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

Answer by robertbu · May 26, 2013 at 03:22 PM

I don't think colliders will work for you given that you want to change the field of view/move camera. Given you know the distance from the camera to the character, you can approach this problem in two ways the first is the one you outline above. Second is to use Viewport coordinates. See answer here for more info on both:

http://answers.unity3d.com/questions/391324/stretching-a-gameobject-to-fit-viewport.html

Note as you change the field of view, you will need to move the camera to keep it in view.

Comment
Add comment · Show 3 · 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 JayC · May 26, 2013 at 03:31 PM 0
Share

Great answer! Could you recommend a different solution than using colliders? I need to keep the camera within boundaries that I can define myself for each level.

avatar image robertbu · May 26, 2013 at 03:53 PM 0
Share

Do it mathematically. That is, with the Viewport conversion you have the boundaries of the camera in world space. I assume you have $$anonymous$$X, maxX, $$anonymous$$Y, maxY for the boundaries for each level. Each time the camera is moved, just check that it does not go past those boundaries. The same is true of zoo$$anonymous$$g. After you change the Field of View or move the camera, calculate the new viewport coordinates. If the positions overlap any of the $$anonymous$$/max values, move the camera.

avatar image JayC · May 26, 2013 at 07:32 PM 0
Share

The issue is that the levels will contain many boundaries. I'm going for a $$anonymous$$ega$$anonymous$$an type level layout. The camera needs to stay within the level where the boundaries are sporadically placed.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Bounds 2 Answers

Keep Cursor In Bounds of Camera 2 Answers

Capture Bounds with an orthographic camera -- runtime world map 1 Answer

Adjust Ortographic Camera Size so it fits objects of my scene automatically 0 Answers

Keeping the camera in bounds 1 Answer


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