• 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 JonManatee · Aug 05, 2010 at 06:01 PM · physicsraycastcollider

Physics.Raycast not checking layermask properly?

I'm having issues with a Physics.Raycast call in some buggy code that I inherited for a project I'm on, where agents trying to Raycast to a "LatchPoint" layer, which is user layer 8.

Here is the code with the variable names made easier to read:

RaycastHit hitInfo;
int myMask = 1 << 8;
if (Physics.Raycast(sourceTransform.position, (raycastTarget - sourceTransform.position).normalized, out hitInfo, Mathf.Infinity, myMask))
{
    storedTransform = hitInfo.transform;
    Debug.Log(storedTransform.name);
}

The Debug.Log will print the name of a CharacterController in the "Default" layer, and not get to the CapsuleCollider inside of it on the "LatchPoint" layer. Am I using my mask incorrectly?

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 skovacs1 · Aug 05, 2010 at 09:39 PM 0
Share

Your code seems good. I have a Linecast written the exact same and it only hits the stuff in the masked layer. I have done this with Raycasts and the mask worked correctly there as well. This also reflects the layer masking example correctly http://unity3d.com/support/documentation/Components/Layers.html. All I can suggest is to recheck your scene. Ensure that the CharacterController being hit is actually on the Default layer and that the CapsuleCollider's transform is named differently. Do you maybe have another CharacterController with that name on layer 8? Try turning off layers to check.

avatar image skovacs1 · Aug 05, 2010 at 09:43 PM 0
Share

Also, make sure that no other debug logs are sending the message since this one will not fire if nothing was hit. Try something like changing your Debug to read something like Debug.Log(String.Format("Raycast Hit (0) on layer (1)", storedTransform.name, storedTransform.gameObject.layer));

1 Reply

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

Answer by JonManatee · Aug 09, 2010 at 06:05 PM

This is what fixed it, although I'm not entirely sure why:

RaycastHit hitInfo;
int myMask = 1 << 8;
if (Physics.Raycast(sourceTransform.position, (raycastTarget - sourceTransform.position).normalized, out hitInfo, Mathf.Infinity, myMask))
{
    storedTransform = hitInfo.collider.gameObject.transform;
    Debug.Log(storedTransform.name);
}
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 wihu · May 31, 2015 at 03:24 PM 0
Share

i am surprised that this bug is still there.. just got the same issue.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Raycast 2D with origin inside the collider 1 Answer

Camera raycast and rotated collider 0 Answers

Passing values from Shader to C# script 0 Answers

Manual collision check 2 Answers

Using different paricle emitters depending on the tag of the object raycast hits? 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