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 L0932 · Aug 18, 2013 at 06:15 AM · camerascreensizeboundary

Set screen boundaries with code

Hello, i'm making a 2d space shooter game and I want to set the screen boundaries for the player using code, which will have the player stop at the edge of the screen for different aspect ratios. How would this be accomplished?

by the way, im trying to avoid using extra gameobjects to use for the boundaries, so please dont include that as part of the solution :)

Comment
Add comment · Show 2
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 getyour411 · Aug 18, 2013 at 06:20 AM 0
Share

Have you looked at Screen.width and Screen.height?

avatar image L0932 · Aug 18, 2013 at 06:33 AM 0
Share

Um yes, here's my code:

 function Update()
 {
 
 //// $$anonymous$$ovement ////
     
     //set screen Bounds
  screenBounds = Vector3(Screen.width, 0, Screen.height);

     //set offset
     screen.x = (screenBounds.x*4)/100; //4% of screenBounds.x
     screen.y = Screen.height;

     
     //$$anonymous$$ove the player. 
     directionH = Input.GetAxis("Horizontal") * speed *   Time.deltaTime;
     playerPos = transform.position;
     playerPos.x = playerPos.x + directionH;
     transform.position = playerPos;
     
     
     //set players position in screen coordinates
     playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);


     //transform.position = Camera.main.ScreenToWorldPoint(Vector3(playerPosScreen.x,screen.y,playerPosScreen.z));
     
     if(playerPosScreen.x > screenBounds.x-screen.x)
     {
         transform.position = Camera.main.ScreenToWorldPoint(Vector3(screenBounds.x-screen.x,playerPosScreen.y,playerPosScreen.z));
     }
     else if(playerPosScreen.x < screenBounds.x/2-screen.x)//not where I want the player at the moment, ignore this
     {
         transform.position = Camera.main.ScreenToWorldPoint(Vector3(screenBounds.x/2-screen.x,playerPosScreen.y,playerPosScreen.z));
         
     }
 }


So Basically, the code here works like I want it to if right wing of the player ship hits the screen boundaries, and cannot go further. This works fine and dandy on some aspect ratio settings, but when I switch to something like 16:9, half the ship is outside the screen..

1 Reply

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

Answer by L0932 · Aug 18, 2013 at 06:56 AM

Oh, nevermind..The code works fine. The background image caused some confusion.

Comment
Add comment · Show 2 · 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 getyour411 · Aug 18, 2013 at 07:50 AM 0
Share

glad to hear it

avatar image zfz.zahid · Apr 21, 2014 at 04:53 AM 0
Share

how did u set boundaries with different devices ratio or screen sizes?

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

18 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

Related Questions

How I get sizeof pixels of object 1 Answer

Camera view different screen sizes? 0 Answers

Orthographic camera cuts off half of screen in Web Player - resolution error? 1 Answer

How to change size of camera preview in Editor? 5 Answers

3D object space occupied in camera 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