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 MapuHoB · Jan 02, 2015 at 08:14 AM · c#raycast

How to find all colliders below mouse?

 public static bool IsTopmost(GameObject go)
     {
         RaycastHit[] hits;
         Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         wp.z = Camera.main.transform.position.z;
         hits = Physics.RaycastAll(wp, Vector3.forward, Vector3.Distance(Camera.main.transform.position, go.transform.position) * 2);
 
         if (hits.Length == 0)
         {
             return false;
         }
 
         GameObject topMostSoFar = hits[0].collider.gameObject;
 
         RaycastHit hit;
         for (int i = 1; i < hits.Length; i++)
         {
             hit = hits[i];
 
             if (Compare(topMostSoFar, hit.collider.gameObject) == -1)
             {
                 topMostSoFar = hit.collider.gameObject;
             }
         }
 
         Debug.Log("finishes method");
         return topMostSoFar.name == go.name;
     }

My camera position.z = -5, all sprites in the game position.z = 0. This method never finishes, it always enters the first if which says there are no colliders under the mouse position. What do I do wrong here?

EDIT: It turns out my mistake is very stupid, I'm using Physics instead of Physics2D for 2d colliders. How can I check same thing but with 2d raycasting?

Comment
Add comment · Show 3
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 tanoshimi · Jan 02, 2015 at 08:40 AM 0
Share

And your gameobjects under the mouse all have colliders on them?

avatar image MapuHoB · Jan 02, 2015 at 08:41 AM 0
Share

Pretty much all of them, yeah. Unless the problem comes from the fact that the colliders are 2d and I'm not using Raycast2d?

avatar image tanoshimi · Jan 02, 2015 at 09:05 AM 0
Share

Yep, that'll do it. Physics2D and Physics are totally differeny systems - you won't detect a 2D collider with a 3D raycast.

2 Replies

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

Answer by MapuHoB · Jan 02, 2015 at 09:50 AM

The solution is to use the 2d methods and variable types everywhere and for the direction of the RaycastAll to use Vector2.zero, unless you put Vector2.zero it;s not going to work.

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
avatar image
0

Answer by tanoshimi · Jan 02, 2015 at 09:25 AM

Ummm. Physics2D.RaycastAll?

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 MapuHoB · Jan 02, 2015 at 09:34 AM 0
Share

$$anonymous$$ost likely it is, just it's not clear to me what am I supposed to write for the direction Vec2.zero or?

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

25 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 avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I make a Raycast attached to my mouse rotate an object with a script attached on the Y axis? 0 Answers

Raycast - arrow stopped by walls (layer) but not by units (layer) 1 Answer

2 raycasts on the fpwalker/camera 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