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 nicntj · Jan 01, 2014 at 11:57 PM · 2dresolutionplatformerpixelcamera movement

2D platform game stop camera from moving once reach end of screen

I'm trying to create a 2d platform game were the camera moves from left to right. I want the camera to stop moving when it reaches the end of the level. I tried stopping the camera when it's greater than or equal to the coordinates that I need. And it works! But if I try my game on different monitors/resolutions you get black space at the end of the level. (It doesn't stop when I want it to stop.) So is there a way to, no matter the resolution of the screen, stop the camera from moving?

Thanks for your help! :D

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

Answer by robertbu · Jan 02, 2014 at 12:14 AM

With an orthographic camera, the orthographicSize is 1/2 of the vertical height the camera sees. You can calculate the half width with:

 var halfWidth = Camera.main.orthographicSize * Screen.width / Screen.height;

The 'halfWidth' is how far (x axis) before the end of your level you need to stop.

Comment
Add comment · Show 9 · 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 nicntj · Jan 02, 2014 at 01:11 AM 0
Share

I don't understand. Based on the camera size how does it know how long my level is?

avatar image robertbu · Jan 02, 2014 at 01:15 AM 0
Share

It doesn't. This just allows you to calculate the position. Say your level ends when x = 120 (I don't know how you have your level define, so I cannot be more precise). You need to stop your camera when the camera's x position is 120 - halfWidth. So whatever is the maximum x value of your level, the camera will be stoped 'halfWidth' before that value.

avatar image nicntj · Jan 02, 2014 at 01:27 AM 0
Share

So would it look something like this (2.12 would be the end of my map):

 if(transform.position.x == 2.12 - halfWidth)
 {
 
      //stop cam
 
 }
 else
 {
 
      //$$anonymous$$ove cam
 
 }
avatar image robertbu · Jan 02, 2014 at 01:33 AM 0
Share

I don't know how you have your code structured. Your code as-is won't work because you are using a direct comparison, and the chance of the value landing exactly on 2.12 - halfWidth is small. What you could do is this:

 if (transform.position.x > 2.12 - halfWidth) {
     transform.position.x = 2.12 - halfWidth;
 }

Another possibility is to just not move the camera if the future position is beyond the end value. Assu$$anonymous$$g you do some sort of calculation for the future position and assign it to 'pos' you could do:

 if (pos <= 2.12 - halfWidth) {
     transform.position = pos;
 }
avatar image nicntj · Jan 02, 2014 at 01:51 AM 0
Share

But the first one would always be true b/c lets say the transform.position.x = 1 well that is greater than 2.12-halfwidth b/c my halfWidth is 640. And then the camera is way off the screen.

Show more comments

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning mobs in a platformer game 0 Answers

I'm having trouble with my character when playing in unity, and when I build and run the game 0 Answers

Has anyone made a successful Pixel Perfect Camera script for platformer games? 2 Answers

Blur on first plane 0 Answers

Adjust camera for different screen sizes 0 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