• 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
Question by Benji_1996 · May 05, 2018 at 12:11 AM · physics2dlayersraycasthit2dlayerlayermask

Physics2D.CircleCast produces no result whenever a layer mask is specified

Here's the code:

 RaycastHit2D $$anonymous$$t;
 int obscuringLayer = 8;
 Vector2 dir = new Vector2 (1f, 0f);
 BoxCollider2D boxCol = GetComponent <BoxCollider2D> ();
 
 boxCol.enabled = false;
 $$anonymous$$t = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f, obscuringLayer);
 boxCol.enabled = true;

The problem is that the CircleCast will not $$anonymous$$t ANYTHING on ANY layer. Strangely enough, t$$anonymous$$s only happens if I specify the layer I want. if I put:

 $$anonymous$$t = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f);

The CircleCast works perfectly (except, of course, that it $$anonymous$$ts objects I don't want it to $$anonymous$$t). I've tried:
-Puting an integer OTHER than the mask for the layer I want it to $$anonymous$$t (so I want it to $$anonymous$$t layer 8 so I tried putting in layer 5 and it still wouldn't $$anonymous$$t anyt$$anonymous$$ng)
-Putting in a LayerMask that filters in ALL layers EXCEPT the one I want
-Putting in a LayerMask that only filters in the one I want
-Adding dynamic and static rigidbodies on the targets
-Testing the targets with isTrigger set to true and false
-Using Physics2D.LineCast with the same layer as an integer parameter
And I tried a few other t$$anonymous$$ngs that I'm really wis$$anonymous$$ng I wrote down right about now...
T$$anonymous$$s always happens every time I try to raycast with a specific layer. I'm just trying to copy the style from Unity's Roguelike tutorial, they have code that looks like t$$anonymous$$s:

 //Disable the boxCollider so that linecast doesn't $$anonymous$$t t$$anonymous$$s object's own collider.
 boxCollider.enabled = false;
             
 //Cast a line from start point to end point checking collision on blockingLayer.
 $$anonymous$$t = Physics2D.Linecast (start, end, blockingLayer);
             
 //Re-enable boxCollider after linecast
 boxCollider.enabled = true;

I can't possibly imagine why on earth t$$anonymous$$s code works perfectly fine but mine does absolutely not$$anonymous$$ng...

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 Bunny83 · May 05, 2018 at 01:31 AM

It's a layer mask, not a layer index. The layermask is a bitmask and since an integer value has 32 bits there are 32 layers. Explanations are here(UA) and here(YT).


The easiest way to specify a layer mask is to use Unity's LayerMask struct. When you create a public variable of that type you can select the layers you want in the inspector. The struct also as a couple of static methods to get the mask for a given layer name or the index for a given layer name.


If you like hardcoding the layers in your script the easiest way in code is to use the bitwise left s$$anonymous$$ft operator. 1<<layerIndex gives you a layer mask value with only the layer with the index "layerIndex". If you want multiple layers in your mask you have to combine the masks using the bitwise "or operator" | like t$$anonymous$$s:

 int mask = (1<<10) | (1<<15);  // layer 10 and 15


Comment

People who like this

0 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 Bunny83 · May 05, 2018 at 01:33 AM 0
Share

Note a value of 8 as layer mask is actually the binary number "0000 1000". So you actually specified layer index "3"

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

86 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

Related Questions

Why is RaycastHit2d Not working 0 Answers

Using Physics2D.IgnoreLayerCollision and LayerMask from Editor 1 Answer

OverlapSphere doesn't detect colliders 1 Answer

Detecting that I'm clicking a unit even though I'm not? 0 Answers

LayerMask for RayCast 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