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 /
avatar image
0
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 hit;
 int obscuringLayer = 8;
 Vector2 dir = new Vector2 (1f, 0f);
 BoxCollider2D boxCol = GetComponent <BoxCollider2D> ();
 
 boxCol.enabled = false;
 hit = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f, obscuringLayer);
 boxCol.enabled = true;

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

 hit = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f);

The CircleCast works perfectly (except, of course, that it hits objects I don't want it to hit). I've tried:
-Puting an integer OTHER than the mask for the layer I want it to hit (so I want it to hit layer 8 so I tried putting in layer 5 and it still wouldn't hit anything)
-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 things that I'm really wishing I wrote down right about now...
This 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 this:

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

I can't possibly imagine why on earth this code works perfectly fine but mine does absolutely nothing...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 shift 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 this:

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


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 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.

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

Physics2D.Linecast ignoring walls 1 Answer

Using Physics2D.IgnoreLayerCollision and LayerMask from Editor 1 Answer

Raycast2D not working with LayerMask 1 Answer

OverlapSphere doesn't detect colliders 1 Answer

Why is RaycastHit2d Not working 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