• 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 /
This question was closed Apr 27, 2018 at 06:09 PM by h00man.
avatar image
0
Question by h00man · Apr 27, 2018 at 02:46 PM · raycasthit

how to distinguish which layer has been hit by raycast first?

hey guys, so i've been trying to make a raycast bullet script, which a single ray could hit different game objects in different layers. i have a gun and boxes in my scene one is stone and the other is a wooden one and when i shoot the ray cast bullet passes through all two boxes.sometimes the stone one is in front sometimes the wooden one. how can i tell the script that the first box in the front should be effected and the one in the back should not? i created a small gif file to show my problem: http://s6.uplod.ir/i/00920/6bcpcmir38ad.gif and here is my code:

 Ray rayshow = new Ray (transform.position, transform.forward);
 RaycastHit hitinfo;
 
 
         if (Physics.Raycast (rayshow, out hitinfo, range, stoneLayers)) {
 
             if (hitinfo.collider != null)
             if (hitinfo.collider.gameObject.layer == LayerMask.NameToLayer ("stone")) {
 
 
                 hitstone ();
 
 
             }
 
 
             if (Physics.Raycast (rayshow, out hitinfo, range, woodLayers)) {
                 
 
                 if (hitinfo.collider != null)
                 if (hitinfo.collider.gameObject.layer == LayerMask.NameToLayer ("wood")) {
                     
                     
 
                         
                     hitwoood ();
 
 
             
 
                 }
 
 
             }
 
 
 
 
         }
     }
     public void hitstone ()
     {
 
 
         Ray rayshow = new Ray (transform.position, transform.forward);
 
 
         Debug.DrawLine (rayshow.origin, hitinfo.point, Color.yellow);
 
 
         targetForGun targeti = hitinfo.transform.GetComponent<targetForGun> ();
 
         if (targeti != null) {
 
             targeti.takedamge (damage);
 
         }
         if (hitinfo.rigidbody != null) {
 
             hitinfo.rigidbody.AddForce (-hitinfo.normal * BulletImpactForce);
 
         }
 
 
         GameObject impatbullet = Instantiate (stoneHitImpact, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));
 
         impatbullet.transform.parent = hitinfo.transform;
 
 
         Destroy (impatbullet, 6f);
         GameObject stonesound = Instantiate (stoneHitSound, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));
 
         impatbullet.transform.parent = hitinfo.transform;
 
 
         Destroy (stonesound, 6f);
 
 
 
 
 
 
 
 
 
     }
 
 
     public void hitwoood ()
     {
 
         Ray rayshow = new Ray (transform.position, transform.forward);
         targetForGun targeti = hitinfo.transform.GetComponent<targetForGun> ();
 
         if (targeti != null) {
 
             targeti.takedamge (damage);
 
         }
         if (hitinfo.rigidbody != null) {
 
             hitinfo.rigidbody.AddForce (-hitinfo.normal * BulletImpactForce);
 
         }
         GameObject impatbullet = Instantiate (WoodHitImpact, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));
 
         impatbullet.transform.parent = hitinfo.transform;
 
         Destroy (impatbullet, 6f);
 
 
 
 
 
         GameObject woodsound = Instantiate (WoodHitSound, hitinfo.point, Quaternion.LookRotation (hitinfo.normal));
 
         impatbullet.transform.parent = hitinfo.transform;
 
 
         Destroy (woodsound, 6f);
 
 
 
     }
 }
 

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

  • Sort: 
avatar image
0
Best Answer

Answer by Priyanka-Rajwanshi · Apr 27, 2018 at 03:17 PM

@h00man Take a layermask with 2 layers and use only 1 raycast. Thus only the object which is in front would be detected. You can put your checks for layer inside.

You can either make the layermask variable public and set in inspector or see the below link for setting in code. https://answers.unity.com/questions/8715/how-do-i-use-layermasks.html

Comment
Add comment · Show 2 · 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 h00man · Apr 27, 2018 at 03:55 PM 0
Share

thanks for responding , i'm sure you have my answer but the link you posted was a little bit confusing for me. my stone layer is on number 8 and the wood on 9 how to you think i should change my scrip

avatar image Priyanka-Rajwanshi h00man · Apr 29, 2018 at 06:12 AM 0
Share

Hi, the easier way as to make the requiredLayers$$anonymous$$ask public and assign the required layers in the dropdown menu in inspector. For script see the code below:

 int layermask1 = 1 << 8;
 int layermask2 = 1 << 9;

 Layer$$anonymous$$ask requiredLayers$$anonymous$$ask;
 void Start()
 {
     requiredLayers$$anonymous$$ask = layermask1 | layermask2;
 }

 void Update()
 {
     Ray rayshow = new Ray(transform.position, transform.forward);
     RaycastHit hitinfo;
 
     if (Physics.Raycast(rayshow, out hitinfo, range, requiredLayers$$anonymous$$ask))
     {
         if (hitinfo.collider != null)
         {
             if (hitinfo.collider.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("stone"))
             {
                 hitstone();
 
             }
             else if (hitinfo.collider.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("wood"))
             {
                 hitwoood();
             }
         }
 
     }
 }

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

82 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

Related Questions

SendMessage and RaycastHit 2 Answers

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

RaycastHit.transform/collider 1 Answer

RaycastHit.normal.y value can't be smaller than 1 1 Answer

Shooting system 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