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 Yaza · Jun 14, 2015 at 01:56 PM · performancecollider2dmousepositiondetectionlines

Mouse click on line - performance question

Hello everybody, I have a really specific question about the best practice in this case:

In my project you are drawing paths by clicking on other paths to chose the start point and use the mouse position as the end point.

Therefore I need to detect a mouse click on a path (line drawn with LineRenderer, consisting of one or two straight segments). Right now I'm looping through all the paths to check if the mouse position is closer to them than a detectionRadius I set.

My question is: Would it be better for the performance to give every path one BoxCollider2D for each segment and detect a mouse hit by Raycasting, or is it okay as I do it now? There will probably be 50 paths or more in the scene at some point.

Here is how it will look like:

alt text

Thanks for your answers!

paths.png (24.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by _Gkxd · Jun 14, 2015 at 02:31 PM

For lines in 3D space, rather than Raycasting, you can render each line using a different color on a separate texture (see Render Texture). For example, render your first line using color 0x000000, your second line using 0x000001, etc. Here is an image example of what your render texture might look like for 3 lines.

alt text

This effectively projects all the lines onto the viewing plane, meaning that you can sample the color of the render texture at the screen-space mouse coordinates to see which line you're over. In the image above, if your mouse is above a white pixel, you know it's over the white line, etc. As a bonus, this also works for curves if you ever need to expand your system to support curves. (Curve intersection is a lot more difficult than line intersection.)

This saves you from raycasting and doing intersection tests, but in exchange, you have more draw calls. Although if you don't care about the color of your lines, you can render them on to the screen normally, using different colors, and just see what color your mouse is over.

The same concept applies to lines in 2D space, but intersection tests in 2D are easier than intersection tests in 3D.


lines.png (4.8 kB)
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 Yaza · Jun 14, 2015 at 03:05 PM 0
Share

Thank you for the answer! Unfortunately, my lines all have the same color and I need to know which line exactly was clicked on. I added a picture of how the lines will look like in my case.

avatar image _Gkxd · Jun 14, 2015 at 03:33 PM 1
Share

In that case you can use a Render Texture ins$$anonymous$$d of rendering it to the screen. There is a function that gives you a temporary render texture to work with. You can draw all of your lines on that texture (which won't show up on screen).

You would have to do some profiling in order to see which way is actually faster though.

Alternatively, if you currently store your lines in a list, you can consider storing them in a spatial data structure ins$$anonymous$$d, like a k-d tree. If you do this, you won't have to loop through all the lines. Unfortunately, I don't think Unity has a k-d tree implementation that you can use, so you would have to write your own.

avatar image Yaza · Jun 14, 2015 at 04:53 PM 0
Share

The k-d tree sounds nice, I will probably go with that. Provided that I understand how it works^^

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Performance of player detection: Colliders/Triggers vs OverlapCircle 2 Answers

EdgeCollider2D or BoxCollider2D? 1 Answer

collider2D.bounds.Contains not working properly 3 Answers

PolygonCollider2D & framerate performance 0 Answers

How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 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