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 sun_spider · Mar 01, 2014 at 09:39 PM · mousecollider2d

Any way to get collider2d bounds?

I am trying to limit an object's movement to the screen via something like this:

     if (keepOnScreen) {
         if (mouseLocation.x < screen.x ) {
             mouseLocation.x = screen.x ;
             }
         else if (mouseLocation.x > screen.width) {
             mouseLocation.x = screen.width;
         }
         if (mouseLocation.y < screen.y) {
             mouseLocation.y = screen.y;
         }
         else if (mouseLocation.y > screen.height) {
             mouseLocation.y = screen.height;
         }
         transform.position = mouseLocation;
     }

This works, but of course the transform.position is the center of the object. I'd like to offset it by collision bounds. I can't set up an invisible wall, because I'm forcing the object position via the mouse and I really don't want to make the object move towards the mouse pointer using physics.

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 robertbu · Mar 01, 2014 at 10:09 PM 0
Share

I'm not aware of any universal way to get the bounds of a Collider2D. It is possible (sometimes jumping through hoops), to get the bounds of specific colliders. I've answered a couple of questions about someone wanting a solution for a specific 2D collider. The answers were a bit involved. The code to find the bounds is far simpler if the object does not rotate.

avatar image sun_spider · Mar 01, 2014 at 10:51 PM 0
Share

Yes without rotation it is clearly simpler :) I suspect getting the bounds of a collider2d universally will have to be something created at an engine level... could be pretty slow otherwise if we're looking at points.

Thanks for your answers today, robertbu :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SmooveB · Apr 12, 2014 at 07:22 PM

http://forum.unity3d.com/threads/211486-Physics2D-Bounds-and-Extents

But that will not work for complex shapes. For that, you could probably loop through PolygonCollider2D.points and record the extreme values for each direction.

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

21 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

Related Questions

Why is OnDrag triggering but not OnMouseDown? 0 Answers

how to put a collider on an object but doesnt block the mouse click on the object behind? 2 Answers

2D Game Mouse Problem 0 Answers

Mouse position to Isometric Grid Tile number 1 Answer

aiming with animation 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