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 Sadiq89 · Jan 22, 2015 at 08:33 AM · raycastselection

How can I hit raycast on only one collider and ignore oter collider types

I have Gameobjects I want to select and the selection itself works with Physics.Raycast. But some objects have a sphere collider additionally to the capsule collider. I only want them to be selectable on the capsule collider. How can I do that? I guess the Ignore Raycast layer is not an option, since that would ignore the whole Gameobject.

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
0

Answer by AndresBarrera · Jan 22, 2015 at 08:59 AM

Use collition layers for your objects. Check this and this. Example:

 int mask = (1 << LayerMask.NameToLayer("Tiles"));
 
 Ray ray = ........;//Init your ray
 RaycastHit rayHit;
 Physics.Raycast(ray, out rayHit, 100, mask);
 //This will test a ray only against objects inside the "Tiles" layer


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 Sadiq89 · Jan 22, 2015 at 01:09 PM 0
Share

Can you explain why this would ignore all other colliders? It seems that the whole object inside the "tiles" layer would be ignored. But I don't want an object to be ignored, I only want to ignore its sphere collider (and maybe others). Otherwise the ignore raycast layer would be a good joice, I guess.

avatar image AndresBarrera · Jan 22, 2015 at 01:26 PM 1
Share

Are you using two colliders on the same GameObject? $$anonymous$$y suggestion would be to put one of your colliders (lets say the sphere) on the main object, and the other (the capsule) on a child object. Put that child object in the layer you want to detect with raycast, but not the parent.

From this point on it will depend on what you are doing and how do you want to keep your hierarchy. You can communicate parent and child objects with scripts, or pass some of your scripts from the parent to the child, or use GetComponentInParent to access some behaviour when you detect the ray hit on the child.

avatar image Sadiq89 · Jan 22, 2015 at 02:03 PM 0
Share

Thank you so much! The answer is great and really helped me.

avatar image
0

Answer by MaximilianPs · Apr 13, 2016 at 09:04 PM

I have the same problem, it's an RTS, the soldier have a box collider used to be selected by the player by click on 'em, but the soldiers have also another very big sphere collider which is used for other game mechanics. So, i need the raycast hit the box collider but not the sphere collider. Any help ? O.o

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity RayCast Selection 1 Answer

Raycast Object Selection 3 Answers

GUI Box not showing up after object clicked 1 Answer

Add force to the selected object 1 Answer

Help - Selection System 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