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 Pleng · Jan 03, 2015 at 04:25 AM · cameramaporthographicorthographic cameralevel-map

Camera to centre on player, but never exceed "map" bounds

I am creating a game which has up to 4 orthographic cameras (for up to 4 players), which take up various amounts of the screen, depending on the amount of players.

2 Cameras

4 Cameras

I have a script which controls all the cameras, setting their position relative to the players they are watching.

What I want to achieve is a simple overhead-runner style of movement, whereby the camera will follow the player, but not go outside the bounds of the map.

alt text

I have the code to move the cameras, within a specific bounds, already in place. The question is, how do I FIND the minimum and maximum values? I have a Map object, placed at 0,0 - so I guess I need need to somehow find the top and bottom corners of that object, in relation to the cameras. How can I do this?

My current code is below:

 if((trackPlayer1 == null))
 {
     camPlayer1.transform.position = this.transform.position;
 }
 else
 {
     camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer1.transform.position.y, minY, maxY), camPlayer1.transform.position.z);
 }
 if((trackPlayer2 == null))
 {
     camPlayer2.transform.position = this.transform.position;
 }
 else
 {
     camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer2.transform.position.y, minY, maxY), camPlayer2.transform.position.z);
 }
 
 if((trackPlayer3 == null))
 {
     camPlayer3.transform.position = this.transform.position;
 }
 else
 {
     camPlayer3.transform.position = new Vector3(Mathf.Clamp(trackPlayer3.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer3.transform.position.y, minY, maxY), camPlayer3.transform.position.z);            
 }
 
 if((trackPlayer4 == null))
 {
     camPlayer4.transform.position = this.transform.position;
 }
 else
 {
     camPlayer4.transform.position = new Vector3(Mathf.Clamp(trackPlayer4.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer4.transform.position.y, minY, maxY), camPlayer4.transform.position.z);
 }



UPDATE

Ok so using the code in this question(link), I have managed to get the boundaries working in the top-left camera, when the cameras are 'square' (as in the 4-player layout). However, the other cameras don't track properly at all, and in the rectangular 2-player mode, the top camera still goes too far left-and right. I'm pretty sure I know exactly which line of code is causing the problem... but I don't know what I need to do to fix it...

 SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
 
 if((trackPlayer1 == null))
 {
     camPlayer1.transform.position = this.transform.position;
 }
 else
 {
     float vertExtent = camPlayer1.orthographicSize;
     float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
 
     float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
     float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
     float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
     float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
 
     camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer1.transform.position.y, bottomBound, topBound), camPlayer1.transform.position.z);
 }
 if((trackPlayer2 == null))
 {
     camPlayer2.transform.position = this.transform.position;
 }
 else
 {
     float vertExtent = camPlayer2.orthographicSize;
     float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
 
     float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
     float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
     float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
     float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
 
     camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z);
 }
 
 if((trackPlayer3 == null))
 {
     camPlayer3.transform.position = this.transform.position;
 }
 else
 {
     float vertExtent = camPlayer3.orthographicSize;
     float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
 
     float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
     float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
     float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
     float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
 
     camPlayer3.transform.position = new Vector3(Mathf.Clamp(trackPlayer3.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer3.transform.position.y, topBound, bottomBound), camPlayer3.transform.position.z);            
 }
 
 if((trackPlayer4 == null))
 {
     camPlayer4.transform.position = this.transform.position;
 }
 else
 {
     float vertExtent = camPlayer4.orthographicSize;
     float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
 
     float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
     float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
     float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
     float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
 
     camPlayer4.transform.position = new Vector3(Mathf.Clamp(trackPlayer4.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer4.transform.position.y, topBound, bottomBound), camPlayer4.transform.position.z);
 }

Further Edit... I have now managed to get the first camera to track even when it's not a square size with the following code. It still doesn't work for any of the other cameras though

 if((trackPlayer1 == null))
         {
             camPlayer1.transform.position = this.transform.position;
         }
         else
         {
             float vertExtent = camPlayer1.orthographicSize;
             float horzExtent = vertExtent * (Screen.width * (camPlayer1.rect.width * 2)) / Screen.height; //I guess the problem is here... but how do I fix this??


4cams.png (1.3 kB)
2cams.png (1.1 kB)
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 Pleng · Jan 11, 2015 at 05:50 AM 0
Share

so, as the main camera is now working as expected regardless of size, I guess that 'all' I need to do now is apply some offsets to leftBound, rightBound, topBound and bottomBound dependent on the rects of the other cams. Any idea how to calculate these offsets?

0 Replies

· Add your reply
  • Sort: 

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

25 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

Related Questions

Animate turning orthographic camera view on and off (like in editor) 0 Answers

Orthographic camera size for 1080p quad @ 1920mx1080m 1 Answer

Trying to use an orthographic camera in interior scenes 0 Answers

How to use ScreenPointToRay for orthographic cameras 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 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