Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
1
Question by jellythedonut · May 30, 2016 at 07:47 PM · raycastlayersraycasthitlayermasklayercollision

How to make raycast only collide with two layers?

I'm trying to implement a way for the raycast to only collide with two types of objects, obstacles and players. I made the appropriate layers, set the player to the Player layer and the obstacle to the Obstacle layer. I also created the sight layer which is used by the game object running the script below, and made sure that the sight layer only collides with Player and Obstacle layers. Then I tried doing this in my code:

 if (Physics.Raycast (from, dirToTarget, out hit, (1 << LayerMask.NameToLayer("Sight")))) {
    Debug.Log ("HIT : " + hit.transform.root.gameObject);
 }

Most of the time the Debug either prints that it hits a player or an obstacle but sometimes it will print that it hits an Enemy, which is the game object that is running the script, and from which the ray originates. The ray is drawn from the center of the Enemy to the center of the Target (the player). I am really confused as to why the raycast collides with the enemy, since I instructed it not to in the layer... How can I fix it so that the raycast only collides with either objects of the Player layer or the Obstacle layer?

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
5

Answer by YoyoMario · Jun 30, 2019 at 08:30 AM

@jellythedonut this is the way to do it...

             if (Physics.Raycast(from, dirToTarget, out hit, (1 << LayerMask.NameToLayer("Sight") | (1 << LayerMask.NameToLayer("OtherLayerMaskName"))))
             {
                 Debug.Log("HIT : " + hit.transform.root.gameObject);
             }
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
2

Answer by JedBeryll · May 30, 2016 at 08:28 PM

Edit / Project Settings / Physics Unity already offers a way to allow different settings for different layers: http://docs.unity3d.com/Manual/class-PhysicsManager.html

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 YoyoMario · Jun 30, 2019 at 08:28 AM 0
Share

Raycast has nothing to do with physics manager as jellythedonut asked.

avatar image
1

Answer by Game_Juice · Oct 18, 2021 at 10:38 AM

//First declare variable

 [SerializeField]
 LayerMask lm;

**//assign maxdistance and lm value

 if (Physics.Raycast(from, dirToTarget, out hit, maxdistance,  lm.value))
       {
                      Debug.Log("HIT : " + hit.transform.root.gameObject);
       }

//Save the script and then select lm layer which objects do want to collide with from unity editor

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

60 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

Related Questions

Use Layer Collision Matrix for Raycast 1 Answer

My layers seem to be completely broken 0 Answers

RayCastHit 2d with layerMask not workin 0 Answers

Raycast hitting layer explicitly told to ignore (and doesn't even have collider!) 1 Answer

How can I raycast gameobjects with specific layer ? 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