Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by xJames720x · Mar 07, 2015 at 01:27 AM · physics2draycasthit2dlayermaskphysics2d.raycast

Raycast2D not working with LayerMask

I am attempting to use a Raycast2D to detect if an attack has hit an enemy. I created a LayerMask for the "Enemy" layer and I have an object with a collider on the "Enemy" layer. I have no problem hitting the collider without using a LayerMask, however when I do, I get nothing back. What am I missing here?

 LayerMask EnemyLayer;
 
 void Start ()
 {
     EnemyLayer = LayerMask.NameToLayer ("Enemy");
 }
 
 public void BasicAttack()
 {
     PlayerVitality.Stamina = 0;
 
     Vector2 attackDirection = CalculateAttackDirection ();
     float attackDamage = CalculateDamage (BasicAttackDamage);
     Debug.Log (EnemyLayer.value);
     RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x + 2, transform.position.y), attackDirection, BasicAttackDistance, EnemyLayer.value);
 
     if (hit.collider != null)
     {
         Debug.Log("You hit: " + hit.collider.gameObject.name);
     }
 }
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
9

Answer by Brunomir · Apr 27, 2015 at 04:28 AM

RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x + 2, transform.position.y), attackDirection, BasicAttackDistance,1 << LayerMask.NameToLayer("Enemy");

Comment
Add comment · Show 3 · 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 KokodokoGames · Sep 08, 2016 at 03:59 PM 0
Share

Thanks, that works! In my case the problem was that I left out the "1 <<" before the layer$$anonymous$$ask. What is this? The documentation doesn't mention this.

avatar image doublemax KokodokoGames · Sep 08, 2016 at 04:16 PM 7
Share

Layer$$anonymous$$ask.NameToLayer() returns an index, a value from 0 to 31. The layer$$anonymous$$ask is a bit vector where you can enable/disable each of the 32 layers by setting an individual bit. The (1 << number) starts with a "1" and shifts that bit to the left to the correct position.

There is also Layer$$anonymous$$ask.Get$$anonymous$$ask() which returns the correct value immediately.

avatar image quibit · Mar 18, 2020 at 12:35 PM 0
Share

Thank you for this. This should really be part of the documentation for Raycast though.

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

24 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

Related Questions

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

Physics2D.Linecast ignoring walls 1 Answer

Why is RaycastHit2d Not working 0 Answers

Bitmask/LayerMask used in raycast not working 2 Answers

2D Raycast Layer Mask not working 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