Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Wilelle · Jun 18, 2015 at 06:15 AM · raycastmasklayerlayer mask

Can't Make Raycast Ignore a Layer

I have an object that is constantly right in front of the player, and now I've added in an ability for the player to make enemies stop moving by pointing at them with Raycast. I therefore need to make the Raycast coming from the player ignore the object in front of him to make sure the ray won't just collide with it. I've given this object its own layer (layer 10), but the Raycast still collides with it.

This is my code. What have I done wrong?

 var Enemy : Transform;
 var targetScript01 : FreezeEnemy = Enemy.GetComponent(FreezeEnemy);
 
 var fwd = transform.TransformDirection (Vector3.forward);
 var hitInfo : RaycastHit = new RaycastHit();
 
 function Update ()
 {
     var mask : LayerMask = 10;
     var fwd = (Enemy.position - transform.position).normalized;
     
     Debug.DrawRay(transform.position + Vector3(0, 1, 0), fwd, Color.red);
     if(Physics.Raycast(transform.position + Vector3(0, 1, 0), fwd, hitInfo, mask.value))
     {
         
         if (hitInfo.collider.gameObject.tag == ("Enemy"))
         {
             targetScript01.Spotted02 = true;
         }
         else
         {
             targetScript01.Spotted02 = false;
         }
         
             }
         }
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

3 Replies

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

Answer by Hellium · Jun 18, 2015 at 06:42 AM

Be careful, if you want to ignore the layer 10, you will have to use the bitwise NOT Operator (~)

 var mask : LayerMask = ~(1 << 10);

However, I would suggest you to put the ennemies in a specific layer, and to make a raycast only in this new layer. Otherwise, your raycast could intersect other elements in your scene (ground, walls, ...) and return "yes" even if no ennemy are in front of your player. This way, you won't have to check the tag condition hitInfo.collider.gameObject.tag == ("Enemy"))

Comment
Add comment · 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
0

Answer by tanyanzhi · Jun 18, 2015 at 06:34 AM

The Physics.Raycast function takes a bitmask, where each bit determines if a layer will be ignored or not.

so you need to change "var mask : LayerMask = 10;" to "var mask : LayerMask = 1 << 10;"

here you could find more details: http://docs.unity3d.com/Manual/Layers.html


sorry, I was wrong,"var mask : LayerMask = 1 << 10;" set whick layer would be raycasted. Hellium's answer is right.

Comment
Add comment · 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
0

Answer by Wilelle · Jun 18, 2015 at 09:11 AM

I found a way around it. Since the object in front of the player is only seen by the Raycast exactly when pointing at an enemy, I simply made it a trigger as well.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast ignore layers except 1 Answer

Layer Mask Not working to open the door 1 Answer

Why does Layer Masking for Raycasts only work on Layer 8? 1 Answer

unity raycast layers in Multiplayer 0 Answers

Animator with mask problem 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