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 /
avatar image
0
Question by Infamous911 · Jul 26, 2011 at 09:35 PM · pausedetectspace invadersscreen edges

Detecting the Edge of the Screen

So I need a way to detect the edges of the screen regardless of it's formatting, for my Space Invaders style game. Or, is it possible to resize the screen so that the game is always the same size?

Also, I need a way to pause the game when you win (or lose), so that the player has time to see the text of "you win" or "you lose" before the application closes through Application.Quit();

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 DaveA · Jul 26, 2011 at 11:02 PM 0
Share

That's two questions, you should ask the 2nd one separately

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Waz · Jul 27, 2011 at 03:33 PM

To stop object at edge of screen:

 Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
 viewPos.x = Mathf.Clamp01(viewPos.x);
 viewPos.y = Mathf.Clamp01(viewPos.y);
 transform.position = Camera.main.ViewportToWorldPoint(viewPos);

Note that as the screen resizes, so does the Camera, so you should only see you original issue if the aspect ratio changes.

Comment
Add comment · Show 9 · 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 Infamous911 · Jul 27, 2011 at 04:04 PM 0
Share

When I do this it gives me 983, 421 (Camera.main.pixelWidth,Camera.main.pixelHeight) as the top right corner. When actually the top right corner (in the game view) is around 13, 6. What could be the problem here?

avatar image Infamous911 · Jul 27, 2011 at 07:56 PM 0
Share

Ok so it seems to be working now but the sphere is still going halfway off the screen before stopping from the clamps. Any way to fix this?

avatar image Infamous911 · Jul 27, 2011 at 07:59 PM 0
Share

Also, how can I check if the object is at the edges so that I can then begin to send it in the opposite direction?

avatar image Waz · Jul 27, 2011 at 09:52 PM 1
Share

The above will clamp the position, which is the pivot point, presumably at the centre of your sphere. Add the radius before converting to viewport space to fix this (bit not you'll then need to convert 4 points, one for each edge of the sphere).

If you want to bounce rather than clamp, you need something like:

 if (viewPos.x < 0) { viewPos.x = 0; speed.x = 1.0; }

assu$$anonymous$$g speed is a variable holding the speed which you move the sphere by each frame.

avatar image Infamous911 · Jul 27, 2011 at 10:33 PM 0
Share

Ok this works perfectly. Just one last thing - how do I position the sphere so that it starts in the upper right corner at the beginning of the game?

transform.position = new Vector3(viewPos.x, viewPos.y, 0); doesn't work because it just puts it at (~1, ~1, 0). Thanks a lot for your help!

Show more comments
avatar image
0

Answer by DaveA · Jul 26, 2011 at 11:04 PM

What do you mean 'edges of the screen'? Do you mean the rendered window that the game runs in, or the physical device's screen? And what do you mean by 'detect'?

If I understand you at all, you may want to operate in Viewport space which has units from 0 to 1, always. See: http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToViewportPoint.html

Comment
Add comment · Show 1 · 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 Infamous911 · Jul 27, 2011 at 12:03 AM 0
Share

By edges of the screen I mean the rendered window that the game runs in. Using the Viewport space gets me nowhere because I'm trying to get this sphere to bounce of the edges of the rendered window regardless of it's size. This Viewport space is giving me values between 0 and 1 so I can't say:

Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);

transform.position = new Vector3(viewPos.x, viewPos.y, 0);

because it puts the sphere at (1, 1, 0) ins$$anonymous$$d of at the top right corner like I want it to.

I need something to tell me what the dimensions of the rendered window is so that if you were to resize the window while playing it the sphere would still start at the corner.

Sorry about any confusion, I'm very new to Unity.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I detect if the game's window has been taken down? 1 Answer

Pause an animation with a button 1 Answer

Pause play button does not work. 0 Answers

Is a global static boolean a good implementation for pausing my game? 2 Answers

Disable game controller while pause is active 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