• 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 Kebabs · Oct 01, 2013 at 10:34 PM · resolutionscreenclampresolution settings

Clamp a player to the edge of screen when the resolution changes

Hi

I am new to using Unity (although my confidence is growing) and I am new to C#. For this project I am working on we are making a side scrolling shooter. Last night i managed to set up my GUI pause menu so it positions itself correctly when the screen resolution changes.

I want to achieve the same thing with my clamping. At the moment I have clamped the player on screen using Mathf.Clamp using this code...

 private int minXValue = -8;
 
 private int maxXValue = 8;
 
 private int minYValue = -3;
 
 private int maxYValue = 5;

     Vector3 pos = transform.position;
     
     pos.y = Mathf.Clamp (pos.y, minYValue, maxYValue);
     
     pos.x = Mathf.Clamp (pos.x, minXValue, maxXValue);
     
     transform.position = pos;

However when the resolution changes naturally the positions I have stated no longer apply (well the Y value works fine). I would like some help to clamp the player to the edge of the screen when the screen resolution changes. Thanks for reading :)

EDIT - Forgot to mention that the clamping code is inside update.

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 Kebabs · Oct 02, 2013 at 04:25 PM 0
Share

I have managed to keep the player on screen when the resolution is different thanks to this code.

     Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
     
     pos.x = $$anonymous$$athf.Clamp01(pos.x);
     
     pos.y = $$anonymous$$athf.Clamp01 (pos.y);
     
     transform.position = Camera.main.ViewportToWorldPoint(pos);

However I am now trying to keep the player just inside the screen edge ins$$anonymous$$d of TO the edge, so the player stays on screen fully (as in the whole mesh of the player and no part of it gets cut off by the edge of the screen) Any tips or advice??

avatar image Kebabs · Oct 02, 2013 at 07:21 PM 0
Share

I have managed to solve my problem. I made if statements for each of my controls (up, down left and right) and turned them off and on again when the player hit the appropriate side. I.$$anonymous$$

if (pos.x <=0.1) { leftControlEnabled = false; }

     else
     {
         leftControlEnabled = true;
     }
avatar image Kebabs · Oct 02, 2013 at 07:22 PM 0
Share

Thanks to robertu - I would vote up if I could. Thank you :)

1 Reply

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

Answer by robertbu · Oct 01, 2013 at 10:53 PM

Your min/max values should be floats, not ints.

A way to find the corners of the screen is to use Camera.ViewportToWorldPoint(); In order to make this work, you need to know the distance from the camera to the players. Then you can do something like:

 void Start() {
    Vector3 lowerLeftCorner = Camera.main.ViewportToWorldPoint(Vector3.zero, dist);
    Vector3 upperRightCorner = Camera.main.ViewportToWorldPoint(new Vector3(1.0, 1,0,dist));
     minXValue = lowerLefCorner.x;
     maxXValue = upperRightCorner.x;
 }

Another solution is to use the ratio of screen width to height. Something like:

 void Start() {
   float halfHeight = (maxYValue - minYValue) / 2.0;
   maxXValue = halfHeight * Screen.width/Screen.height;
   minxXalue = -maxXValue;
 }

Note this second solution assumes the camera is centered left/right on the X axis (as indicated by your -8/8).

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 Kebabs · Oct 02, 2013 at 02:52 PM 0
Share

Thank you for your reply and information. I am still finding it hard to understand this. I tried the first solution. What is dist meant to be? Is it meant to be the new vector 3 position?

And for the second solution I get an error which informs me that $$anonymous$$XValue cannot be used in this context.

Any more help :). Thanks.

avatar image Kebabs · Oct 02, 2013 at 03:58 PM 0
Share

I have now actually got the clamping to work on different resolutions. THAN$$anonymous$$S!! However what happens now is that the player gets pushed slightly off screen but I want the players mesh in its entirety to stay on screen, any ideas dude??

avatar image robertbu · Oct 02, 2013 at 04:29 PM 0
Share

First 'dist'. As mentioned in the writeup, 'dist' is the distance form the camera to the players. Your original code clamped the horizontal distance to -8 and 8. This would only work at a certain distance. $$anonymous$$ove the character closer to the camera, and it would go out of the camera view when at 8/-8. $$anonymous$$ove it away from the camera, and there will be extra space (assu$$anonymous$$g a perspective camera). With the default setup with the camera setting at -10 on the z axis and pointing to positive 'z', if your action occurred at Z=0, 'dist' would be 10.

As for your issue of keeping the character fully visible, I don't know why, but I have some ideas. Probably best to have you post your code and let me take a look.

avatar image Kebabs · Oct 02, 2013 at 04:43 PM 0
Share

Thanks for clearing that up :)

As mentioned in my edited question I have now clamped the player and I am now working on keeping them on screen fully. At the moment the player can go to this point...

alt text

But I want them to stop at this point

alt text

As requested here is my code. Just to note - the private bool playerControlEnabled is just they so I could stop the player at the exact width I wanted.

First how I am moving the player...

 if (playerControlEnabled)
     {
     shield.Translate(Vector3.right * shieldSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
     
     
     shield.Translate(Vector3.down * shieldSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
     }

Normally this is not in an if statement. And this is how they are clamped...

     Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
     
     pos.x = $$anonymous$$athf.Clamp01(pos.x);
     
     pos.y = $$anonymous$$athf.Clamp01 (pos.y);
     
     transform.position = Camera.main.ViewportToWorldPoint(pos);
     
     if (pos.x <=0.1)
     {
         playerControlEnabled = false;
     }

I was thinking of using the x position in an if statement to stop the player. What would be ideal is if I was able to prevent the player moving left when they hit 0.1 on the x axis but was still able to move in all other directions.

untitled-1.png (7.5 kB)
untitled-2.png (5.7 kB)

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

14 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

Related Questions

Get the originial Screen resolution, after Screen.SetResolution in mobile? 0 Answers

Pick sensible resolution for FullScreen Mac App 2 Answers

Fitting Into Different Screens 0 Answers

How to bypass resolution limits in window mode 0 Answers

Set Screen Resolution. 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