• 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 /
  • Help Room /
avatar image
Question by lPVDl · Oct 31, 2017 at 03:25 PM · raycastcolliderignoreraycastallexclude

Exclude colliders from raycast

What do I need is to ignore some colliders during raycast (objects which I want to ignore is on the same layer with objects which I want to hit. For example, I have AI which uses colliders to detect enemies, but I don't want its own collider to block the view.)

So, in general I need to use Physics.RaycastAll, exclude collider which I want to ignore and be happy, but it seems to work very slowly. So, what I have tested that is other ways to ignore colliders with using simple Physics.Raycast:

1. Just disable colliders, then enable.
2. Disable game objects, then enable.
3. Change game object layer, then revert.


And here results for 1000k raycasts: alt text

Code which I used - https://pastebin.com/2TwNt0u8



So, changing layers and then rolling back seems to be the most optimal solution. So, the question - is there any better solution?

Comment

People who like this

0 Show 0
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

Answer by Desoro · Oct 31, 2017 at 03:34 PM

Put the enemies on their own layer and Raycast to only that layer. It is also worth noting that Raycasts that start inside a collider, will ignore said collider.

Comment

People who like this

0 Show 13 · 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 lPVDl · Oct 31, 2017 at 08:22 PM 0
Share

Enemies are already on their own layer. Raycast is not going from inside.

avatar image Desoro lPVDl · Oct 31, 2017 at 08:26 PM 0
Share

So if the collider on the AI that detects enemies isn't on the same layer as the enemies, you have no issue. Just Raycast to only the enemy layer using a mask.

But this contradicts your statement "objects which I want to ignore is on the same layer with objects which I want to hit".

avatar image lPVDl Desoro · Oct 31, 2017 at 08:31 PM 0
Share

All objects are on the same layer. If object will raycast it will detect itself.

Show more comments
avatar image Desoro lPVDl · Oct 31, 2017 at 09:42 PM 0
Share

#1 Layering

  • LayerA = AI's layer

  • LayerB = enemy layer

------OR--------

#2 Ignore Self

  • LayerA = all entities (AI is also an enemy)

  • Start Raycast at the center point of the collider on AI. This will ignore the collider on the AI.

avatar image lPVDl Desoro · Oct 31, 2017 at 09:57 PM 0
Share
  1. AI and enemy is the same, there is no difference between them. As I said, there are just colliders which are used to detect entities, the problem is that owned collider can obstruct raycast, rude layering will not help here. 2. There is no way to throw ray from the center (there are more than one collider per object).

Show more comments

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to get raycast to ignore objects behind a panel 0 Answers

C# Door Script Problem 0 Answers

Is it possible to detect ray collisions only with inner side of an object? 0 Answers

RayCast not accurate on non-colliders (3D) 1 Answer

Raycast going through 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