• 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 wb6089967 · Apr 22, 2021 at 05:21 PM · camerascripting problemscripting beginnercamera followcamera movement

How to get the camera's size in world units

Hello, I have a scripting problem I'm trying to get past. So, I want my camera in my 2D game to follow the player until he/she reaches the set right and left ends of the map, but I can't figure out how to get the proper property to do this. Here's the code I'm using:

// constant "RightWorldBoundary" = 35f; if (transform.position.x - camera. / 2 > Level.RightWorldBoundary) { return Level.RightWorldBoundary - camera. / 2; } // constant "LeftWorldBoundary" = -35f; else if (transform.position.x + camera. / 2 < Level.LeftWorldBoundary) { return Level.LeftWorldBoundary + camera. / 2; }

As another potentially useful note, I'm using a 16:9 ratio currently and I have a camera size of 10. What should I use in the __ space to get the width of the camera in world units and have the same type of measurement as my other factors like the transform.position?

Comment
Add comment · Show 1
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 wb6089967 · Apr 22, 2021 at 05:26 PM 0
Share
 // constant "RightWorldBoundary" = 35f;
 if (transform.position.x - camera._______ / 2 > Level.RightWorldBoundary)
 {
     return Level.RightWorldBoundary - camera._______ / 2;
 }
 // constant "LeftWorldBoundary" = -35f;
 else if (transform.position.x + camera._______ / 2 < Level.LeftWorldBoundary)
 {
     return Level.LeftWorldBoundary + camera._______ / 2;
 }

Here's a clearer view of my code (hopfully! I'm kina new to this).

1 Reply

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

Answer by wb6089967 · Apr 22, 2021 at 06:51 PM

Okay, I've got it now. So, the "Size" property of the camera represents half of the camera's height in world units. this is accessed by using camera.orthographicSize the height is already halved for you, so that's not something you need to do. to convert from half the height to half the width, you just need to multiply camera.orthographicSize by camera.aspect, as I've done here:

         camera = UnityEngine.Camera.main;
         halfViewport = (camera.orthographicSize * camera.aspect);

here are the following edits I've made to my code with this in mind:

     private float WorldBourdaryReached()
     {
         float playerXPosition = player.transform.position.x;
         Debug.Log(playerXPosition);
         if (playerXPosition + halfViewport >= Level.RightWorldBoundary)
         {
             return Level.RightWorldBoundary - halfViewport;
         }
         else if (playerXPosition - halfViewport <= Level.LeftWorldBoundary)
         {
             return Level.LeftWorldBoundary + halfViewport;
         }
         else
             return 0f;
     }

In my main update method, if the return of this is 0 (the last statement), I'll just use the player's x position as the camera's x, but if the return is anything but 0 (which will be the maximum the camera can move in either direction), I set the camera's x to the return value.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

266 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How would I go about making a camera system for a game akin to the starfighter mode of Battlefront 2? 1 Answer

Smooth camera script looking at left hand side of character. 0 Answers

Rotatearaound a moving object 1 Answer

"Follow Target" script with a prefab (C#) Help me please! 1 Answer

Problem with the camera, 2D. 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