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
3
Question by abc212 · Mar 04, 2011 at 12:09 PM · cameramovementclamp

How can I clamp a GameObject to screen borders?

I am making a 2D top down game, and I'd like to make it when the player hits the edge of the screen, he cant move any more. I don't wanna use colliders for this, due to screen changing with aspect ratio, I'm sort of a semi begginer with code and I would like to find out the easiest way around this.

What I was thinking like ScreenToWorldPoint

probably an easier way plz help thanks =D

Comment
Add comment
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

2 Replies

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

Answer by Statement · Mar 04, 2011 at 12:28 PM

  • You could have 4 colliders and update them to be located about the points of the screen corners, stretching to the next, having a layer mask that only blocks the player.

  • You could build a mesh that span the frustum of the camera every time the screen resolution is changed, and use a mesh collider (or multiple convex colliders if you would get tunneling). Then you could just parent this to the camera so it follow the camera.

  • Another variant would be to sample each corner to create 4 planes (or lines if it's strictly 2D) and test movement against those planes.

  • A fourth variant would be similar to the previous one; Get the line from the current position to its desired position and clamp it where it meets the screen edge. If you sample the screen locations for "current" and "next" position of your object, you can find out how far in screen space it moves before it's clamped and apply the same scale in world space, to derive the clamped position that way.

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 abc212 · Mar 05, 2011 at 01:29 AM 0
Share

i think I'm going to stick with the frustum method, thanks for your reply.

avatar image Statement · Mar 05, 2011 at 11:08 AM 0
Share

Note that if the camera is moving, then you might get tunneling effects (the player escaping the bounds), when using a simple mesh collider. You might want to construct convex mesh colliders (extrude the colliders out a bit, reaching outside the field of view

avatar image t_w · Sep 29, 2015 at 01:09 AM 0
Share

Hi, Statement, I want to set 4 colliders and locate them at the border, but I did not solve how to archieve it. Do you know how to write in the codes?

avatar image
1
Best Answer

Answer by PrimeDerektive · Mar 04, 2011 at 04:55 PM

Statement pretty much covered it; I would personally use the frustrum planes option (his second bullet point), you could probably make use of this function.

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 abc212 · Mar 05, 2011 at 01:25 AM 0
Share

Just what i needed thank u very much, now to get my head around using it properly...

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

2 People are following this question.

avatar image avatar image

Related Questions

Camera Zoom script stops ability to pan 1 Answer

2D Orthographic camera movement clamp in any orthographic size 1 Answer

RTS Camera Rotation and Movement 0 Answers

Camera movement question 1 Answer

Clamping Issue 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