Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Paikz · Apr 08, 2014 at 11:59 AM · camera2dspritebackgroundbounds

Keep Top Down 2D camera in bounds of background sprite

Hi.

I'm trying to make a 2D top down camera for my game. I'm a beginner but I managed to solve it for 1 background sprite. My soloution was not a general soloution though and it only works for that particular sprite. I would kindly accept any help of how I would be able to calculate the bounds of any sprite and use it in my script.

This is what I've done so far:

using UnityEngine;

using System.Collections;

public class CameraBounds : MonoBehaviour

{

//I write the value of the bound in the inspector

 public float rightBound;
 public float leftBound;
 public float topBound;
 public float bottomBound;
 private Vector3 pos;

 private Transform target;

 // Use this for initialization
 void Start () 
 {
     target = GameObject.FindWithTag("Player").transform;
 }
 
 // Update is called once per frame
 void Update () 
 {
     Debug.Log(pos);

     transform.position = pos;

     if(pos.x != rightBound || pos.x != leftBound || pos.y != topBound || pos.y != bottomBound)
     {
         pos = new Vector3(target.position.x, target.position.y, transform.position.z);
     }

     //Right

     if(pos.x >= rightBound)
     {
         pos.x = rightBound;            
     }

     //Left

     if(pos.x <= leftBound)
     {
         pos.x = leftBound;
     }

     //Top

     if(pos.y >= topBound)
     {
         pos.y = topBound;
     }

     //Bottom

     if(pos.y <= bottomBound)
     {
         pos.y = bottomBound;
     }


 }

}

Comment
Add comment · Show 3
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 joshpsawyer · Apr 11, 2014 at 01:58 AM 0
Share

For clarification: Do you want the camera to be bounded by the boundaries of the background sprite, so that you can't see beyond its edges?

avatar image robertbu · Apr 11, 2014 at 06:26 AM 0
Share

Without knowing your game mechanic it is difficult to tell, but it seems to me that there are issues with the 'if' statement on line 22. And you can simplify this code some. I think you want something more like:

 void Update() {
     var pos = new Vector3(target.position.x, target.position.y, transform.position.z);
     pos.x = $$anonymous$$athf.Clamp(pos.x, leftBound, rightBound);
     pos.y = $$anonymous$$athf.Clamp(pos.y, bottomBound, topBound);
     transform.position = pos;
 }

If you wanted to make this general code you would need to use the bounds of the sprite and the orthographic size of the camera. You can get the bounds using the SpriteRenderer.sprite.bounds. The Orthographic size is 1/2 the vertical size seen by the camera. You can get the horizontal size by using the aspect ration of the camera.

avatar image Paikz · Apr 13, 2014 at 07:33 PM 0
Share

Joshpsawyer: Correct

Robertbu: Thank you for introducing me to mathf.clamp. The code you provided worked really well and I replaced it with my former code.

I have also got hold of the sprites bounds and vertical / horizontal size but I do not know how to use them properly. Can you explain how I should use the values to make it general? :)

Thanks in advance.

3 Replies

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

Answer by Paikz · Apr 14, 2014 at 01:48 PM

Solved it after the answers I've been given + some googling.

Here's the script I came up with:

 private float rightBound;
 private float leftBound;
 private float topBound;
 private float bottomBound;

 private Vector3 pos;
 private Transform target;
 private SpriteRenderer spriteBounds;
 
 // Use this for initialization
 void Start () 
 {
     float vertExtent = Camera.main.camera.orthographicSize;  
     float horzExtent = vertExtent * Screen.width / Screen.height;

     spriteBounds = GameObject.Find("1 - Background").GetComponentInChildren<SpriteRenderer>();

     target = GameObject.FindWithTag("Player").transform;

     leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
     rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
     bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
     topBound = (float)(spriteBounds.sprite.bounds.size.y  / 2.0f - vertExtent);

 }
 
 // Update is called once per frame
 void Update () 
 {
     //Debug.Log();

     var pos = new Vector3(target.position.x, target.position.y, transform.position.z);
     pos.x = Mathf.Clamp(pos.x, leftBound, rightBound);
     pos.y = Mathf.Clamp(pos.y, bottomBound, topBound);
     transform.position = pos;
 }

 void OnLevelWasLoaded()
 {
     Start();
 }

Helpfull link: http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html

Thanks for the help guys (Joshpsawyer & robertbu)! I'm glad you took the time to help me out. :)

Comment
Add comment · Show 4 · 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 joshpsawyer · Apr 14, 2014 at 06:32 PM 0
Share

No problem! You should mark your answer as accepted so that the question won't remain active.

avatar image Avaciin · Mar 10, 2015 at 05:57 PM 0
Share

Thank you for this!!

avatar image Lewinow · Feb 20, 2018 at 04:13 PM 0
Share

Sorry to bother, but there's a tiny error in this code, line 13:

float vertExtent = Camera.main.**camera.**orthographicSize;

This camera. does not compile; I'm just posting this here. Good day,

Lewinow

avatar image Owen-Reynolds Lewinow · Feb 20, 2018 at 04:51 PM 0
Share

Everything from more than a few years ago will have that problem. Now you have to use GetComponent<Camera>(). But back then dot-camera (as written above) was a shortcut. You have the same problem with rigidbodies, renderers ... all those had a shortcut, which is now gone.

avatar image
2

Answer by MorphVGX · Aug 03, 2016 at 09:51 PM

This is what worked for me to get the limits for the camera position:

 float camExtentV = _cam.orthographicSize;  
 float camExtentH = (camExtentV * Screen.width) / Screen.height;
 
 Bounds levelBounds = LevelBoundsSpriteRenderer.bounds;
 
 float leftBound = levelBounds.min.x + camExtentH;
 float rightBound = levelBounds.max.x - camExtentH;
 float bottomBound = levelBounds.min.y + camExtentV;
 float topBound = levelBounds.max.y - camExtentV;    
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
avatar image
0

Answer by Simon-Larsen · Jul 19, 2021 at 12:01 AM

I needed something like this, but a tiny bit different. My camera had to support zooming, so initialization in Start was not sufficient. Adjusted your sample code to support a camera with dynamic bounds and added the possibility for padding.

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class BoundedCamera : MonoBehaviour
 {
     public SpriteRenderer spriteBounds;
     // Pad the bounded area to 'paddingRatio'. Such that 1 means the camera can fully reach the bounds and 0.95 provides a 5% padding on all ends
     public float paddingRatio = 1f;
     private new Camera camera;
 
     void Start()
     {
         camera = GetComponent<Camera>(); 
     }
 
     void LateUpdate()
     {
         float verticalExtent = camera.orthographicSize;
         float horizontalExtent = verticalExtent * Screen.width / Screen.height;
         float spriteWidth = spriteBounds.sprite.bounds.size.x / 2.0f;
         float spriteHeight = spriteBounds.sprite.bounds.size.y / 2.0f;
 
         var leftBound = spriteBounds.transform.position.x + paddingRatio * (horizontalExtent - spriteWidth);
         var rightBound = spriteBounds.transform.position.x + paddingRatio * (spriteWidth - horizontalExtent);
         var bottomBound = spriteBounds.transform.position.y + paddingRatio * (verticalExtent - spriteHeight);
         var topBound = spriteBounds.transform.position.y + paddingRatio * (spriteHeight - verticalExtent);
 
         var pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         pos.x = Mathf.Clamp(pos.x, leftBound, rightBound);
         pos.y = Mathf.Clamp(pos.y, bottomBound, topBound);
 
         transform.position = pos;
     }
 }

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

27 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

Related Questions

Background in 2D game: sprite or image? 0 Answers

Scrolling background wall collision problem 0 Answers

How to draw background image on camera without MVP Matrix 0 Answers

[Beginner Question] Loading, Destroying and Replacing UI Background Sprites (2D) 0 Answers

Unity 2D camera Bounds for player 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