Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DOelbaum · Dec 03, 2016 at 09:35 AM · raycastlayersdebuglayermask

Raycast hitting layer explicitly told to ignore (and doesn't even have collider!)

Hey friends,

I have a raycast emanating from an enemy towards my player. The code calling the raycast is as follows and is called in the enemy's script:

     void FireZeMissile(){
         
         Debug.Log("BOOM");
         int layerMask = (1 << 10) | (1 << 12) | (1 << 23);
 
         layerMask = ~layerMask;
 
         RaycastHit hit;
         Ray shootRay = new Ray(transform.position, player.transform.position - transform.position);
         
         if (Physics.Raycast(shootRay, out hit, 1000f, layerMask, QueryTriggerInteraction.Collide)){
             if (hit.transform.gameObject.tag == "Shield"){
                 player.gameObject.GetComponent<PlayerHandler>().shieldscript.TakeDamage(damage);
                 Debug.Log("Shield hit.");
             }
             Debug.Log(hit.transform.gameObject.name + ", layer: " + hit.transform.gameObject.layer;
             Debug.DrawLine(shootRay.origin, hit.point, Color.red, 100f);
         }
     }


My "BOOM" debug message gets hit of course and the shield debug never gets hit. Incredibly, the last Debug.Log reads "FPSController, layer: 23". How is this possible?? Didn't I just tell it NOT to hit layer 23 when I set up my layermask? Why could this be happening?? Additionally, "FPSController" does NOT have a collider. It has a child that does but that child is layered as "Player" (layer 11).

Does anyone know what is happening here?

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 TimLoo6 · Mar 31, 2020 at 11:33 AM 0
Share

Facing the same problem and apparently no one was able to find a solution xD

avatar image Owen-Reynolds TimLoo6 · Mar 31, 2020 at 03:59 PM 0
Share

hexagonius's answer is correct.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Dec 03, 2016 at 11:26 AM

This is called compound collider. I assume, you have a Rigidbody on your FPSController. a rigidbody combines alle colliders under it into one big collider referencing itself when any collision is detected. So the child CAN be hit due to layer 11, but the collider belongs to your FPSController, which is why your log is logging what it logs (??? ;) )

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 Owen-Reynolds · Dec 04, 2016 at 04:01 AM 0
Share

In more detail, the raycast hits the exact collider it should, no nonsense, using the layer for that exact individual collider, and raycasthit.collider tells you that.

But then raycasthit.transform is really a helper function. It climbs the tree to a compound collider parent (the first rigidbody it sees, I think.) It's never been documented that I know of.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use Layer Collision Matrix for Raycast 1 Answer

My layers seem to be completely broken 0 Answers

RayCastHit 2d with layerMask not workin 0 Answers

How to make raycast only collide with two layers? 3 Answers

How to add color to the environment only, without characters 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