Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Hybrid_Gamez · May 02, 2016 at 02:47 PM · unity 5unity 2d2d-platformercamera-movementcamera follow

How to set Camera Limits in Unity that would stay accurate for different screen ratios?

So I have a script that follows the player and has xMin, xMax, yMin, YMax floats that limit the space in which the camera can move around in, but my problem is that the values in xMax, etc... Won't be accurate when for example you change from a 16:9 ratio to a 4:3 ratio, I tried to figure out some algorithm that would get the Screen.width and then multiply it by a number, but no hope...

Any help/tips very much appreciated

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 Hybrid_Gamez · May 02, 2016 at 02:48 PM 0
Share

Also I am using an Orthographic camera

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Andrii · May 02, 2016 at 03:16 PM

I think you're on a right path. If you divide Screen.height by Screen.width and then multiply it by orthographicSize of the camera, you can calculate current width of the camera in world units. Then don't let the camera go beyond xMin + width of the camera and xMax - width of the camera.

I hope I got everything right :)

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 Hybrid_Gamez · May 02, 2016 at 04:07 PM 0
Share

Thanks for the answer :) so you mean, as in code something like this?

float x = Screen.height/Screen.width * 6.5

float x$$anonymous$$in += x

avatar image Andrii Hybrid_Gamez · May 02, 2016 at 05:24 PM 0
Share

I'm not 100% sure. Try it out and see. Also, you can get camera size using code, like this, to not put the number in manually: Camera.main.orthographicSize

avatar image Hybrid_Gamez Andrii · May 03, 2016 at 10:20 AM 0
Share

Okay, I tried it, but what exactly should the x$$anonymous$$ax value be? :)

avatar image
0

Answer by sajjadgameactor · Feb 17, 2021 at 10:03 PM

I write a simple and reliable script for my game to handle camera drag and swipe for any aspect ratio. Everyone can use this code easily :)

 using UnityEngine;
 
 public class CameraDragController : MonoBehaviour
 {
     [SerializeField] private Vector2 xBoundWorld;
     [SerializeField] private Vector2 yBoundWorld;
     [SerializeField] public bool HorizentalDrag = true;
     [SerializeField] public bool VerticalDrag = true;
     [SerializeField] public float speedFactor = 10;
 
     private float leftLimit;
     private float rightLimit;
     private float topLimit;
     private float downLimit;
 
     public bool allowDrag = true;
     private void Start()
     {
         CalculateLimitsBasedOnAspectRatio();
     }
 
     public void UpdateBounds(Vector2 xBoundNew, Vector2 yBoundNew)
     {
         xBoundWorld = xBoundNew;
         yBoundWorld = yBoundNew;
         CalculateLimitsBasedOnAspectRatio();
     }
 
     private void CalculateLimitsBasedOnAspectRatio()
     {
         leftLimit = xBoundWorld.x - Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
         rightLimit = xBoundWorld.y - Camera.main.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
         downLimit = yBoundWorld.x - Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
         topLimit = yBoundWorld.y - Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;
     }
 
     Vector3 lastPosView; // we use viewport because we don't want devices pixel density affect our swipe speed
     private void LateUpdate()
     {
         if (allowDrag)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 lastPosView = Camera.main.ScreenToViewportPoint(Input.mousePosition);
             }
             else if (Input.GetMouseButton(0))
             {
                 var newPosView = Camera.main.ScreenToViewportPoint(Input.mousePosition);
                 var cameraMovment = (lastPosView - newPosView) * speedFactor;
                 lastPosView = newPosView;
 
                 cameraMovment = Limit2Bound(cameraMovment);
 
                 if (HorizentalDrag)
                     Camera.main.transform.Translate(new Vector3(cameraMovment.x, 0, 0));
                 if (VerticalDrag)
                     Camera.main.transform.Translate(new Vector3(0, cameraMovment.y, 0));
             }
         }
     }
 
     private Vector3 Limit2Bound(Vector3 distanceView)
     {
         if (distanceView.x < 0) // Check left limit
         {
             if (Camera.main.transform.position.x + distanceView.x < leftLimit)
             {
                 distanceView.x = leftLimit - Camera.main.transform.position.x;
             }
         }
         else // Check right limit
         {
             if (Camera.main.transform.position.x + distanceView.x > rightLimit)
             {
                 distanceView.x = rightLimit - Camera.main.transform.position.x;
             }
         }
 
         if (distanceView.y < 0) // Check down limit
         {
             if (Camera.main.transform.position.y + distanceView.y < downLimit)
             {
                 distanceView.y = downLimit - Camera.main.transform.position.y;
             }
         }
         else // Check top limit
         {
             if (Camera.main.transform.position.y + distanceView.y > topLimit)
             {
                 distanceView.y = topLimit - Camera.main.transform.position.y;
             }
         }
 
         return distanceView;
     }
 }
 

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

84 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

Related Questions

How to stop the camera when the player has reached the edge of the level? 2 Answers

2D Sample Camera Script malfunctioning 0 Answers

localScale.x Affecting Player Translations 0 Answers

2D Collision Best Practice 0 Answers

Camera yMin postion should be restricted to player postion 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