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
1
Question by blitzen · Oct 19, 2011 at 04:45 AM · 2dcolliderscreenrect

Collider to Screen Space: Determining if it's Within a Rect

The task is to determine whether any itty bitty part of an object's collider lay within a Rect on the screen.

Currently, my jury-rigged solution is to do a ray cast out from the center of the Rect to wherever on the 3D terrain it hits and for each object, find its Collider.ClosestPointOnBounds() to the hit point, project that point back to screen space, and then test if it lay within the rect. This works over 90% of the time, but there would be some cases where it would be off, like if it's an FPS and the ray hit a very distant or very close wall. In that case the ClosestPointOnBounds would appear more to the center of the object relative to the camera, and if the Rect's side was between this point and the collider's far edge, it wouldn't catch it. Not to mention, it seems clumsy to go casting a ray and doing all these 3D calculations just to bring it all back to 2D again.

I'd imagine that this is a relatively common task, and that there's a more clean-cut way to do it. Since Unity has already written a Collider.ClosestPointOnBounds method for 3D space, I would reckon it to be both simpler and less cpu-intensive to calculate it in 2D.

Thus my question is, how can one project an entire collider into 2D first, and then find the closest point of that 2D shape to a given point, or otherwise generally determine if the collider is at least partially within a bounding screen Rect?

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 SomeRandomGuy · Jul 16, 2012 at 02:23 PM 0
Share

I've been wondering about something similar here, I used a spherecast from my camera, but this has pretty much the same effect, when too close(or too far) the spherecast will react to objects outside of my 2D space.

$$anonymous$$ight be an idea to change the radius of the spherecast depending on the distance it travels before hitting something, but I'm not sure whether or not this is possible. Hope this or my question will be answered sometime, since they are essentially the same.

avatar image dannyskim · Jul 16, 2012 at 05:53 PM 0
Share

You may want to consider using Unity's built in functions for this, such as $$anonymous$$onobehaviour.OnBecameVisible and $$anonymous$$onobehaviour.OnBecameInvisible

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Jul 19, 2012 at 06:41 PM

you are right that there's two main ways to compare 2d (screen) and 3d (mesh)

  • translate 2d to 3d and compare in 3d

  • translate 3d to 2d and compare in 2d

first about 2d - i'm not sure that comparing two rasterized 2d image to check it's intersects is less expensive then the next method.

second - 3d comparison. i think the easiest way is to make a simple collider (6 surfaces or 12 tris) that repeats camera's rect in 3d world. collider will look like camera's view area in scene view, but will be smaller a little - proportonal to your rect size. then make this collider as trigger and detect all collisions with it by script.

IMHO this is a very easy to calculate method, especially if you disable all unneeded collider interactions based on layers.

PS use a box collider instead of mesh if your camera is orthogonal

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't move unless in BoxCollider2D (C#) 1 Answer

How would I keep my player from going past the background? 5 Answers

RayCasts and BoxCollider2D not working? 2 Answers

One Rect in 2 different places 0 Answers

Combining multiple box colliders into one edge collider 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