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
1
Question by Der_Kevin · Dec 08, 2015 at 11:57 AM · raycastraycasthitray

set ray only when raycast a specific layer

hey! i want to debug a raycast, but only when it hits a specific layer, if it hits somehwere else, do nothing. here is the code so far:

 using UnityEngine;
 using System.Collections;
 
 public class RaytoMouse : MonoBehaviour {
 
     public float distance = 50f;
 
     //replace Update method in your class with this one
     void FixedUpdate () 
     {    
         //if mouse button (left hand side) pressed instantiate a raycast
         if(Input.GetMouseButton(0))
         {
             //create a ray cast and set it to the mouses cursor position in game
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, distance)) 
             {
                 //draw invisible ray cast/vector
                 Debug.DrawLine (ray.origin, hit.point);
                 //log hit area to the console
                 Debug.Log(hit.point);
                 
             }    
         }    
     }
 }
 
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
24
Best Answer

Answer by wibble82 · Dec 08, 2015 at 12:05 PM

The ray cast system allows you to specify a layer mask. It's described in detail in the docs:

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

But they don't show an example.

The simplest way to use it is to make use of the layer mask utility in unity:

 //get the mask to raycast against either the player or enemy layer
 int layer_mask = LayerMask.GetMask("Player", "Enemy");

 //or this would be just player
 //int layer_mask = LayerMask.GetMask("Player");
 
 //or this would be player, enemy or cows!
 //int layer_mask = LayerMask.GetMask("Player","Enemy","Cows");
 
 //do the raycast specifying the mask
 if (Physics.Raycast (ray, out hit, distance, layer_mask))
 {
 
 } 

The docs for it are here:

http://docs.unity3d.com/ScriptReference/LayerMask.html

Basically the layer mask is just an integer. For each layer in your game (up to 32), a corresponding 'bit' is set in your mask. The raycast system can use this 32 bit mask to work out what layers you're interested in. Unity's LayerMask utility is a convenient way of constructing these masks so you don't have to think too hard about bits and masks and stuff!

-Chris

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 Tapspringger · Sep 28, 2017 at 12:19 AM 0
Share

Very good answer, saved me a lot of time! Nice Work.

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

35 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

Related Questions

Raycasting and foreach loops 1 Answer

set ray.point as x, y and z float 1 Answer

RaycastHit.transform/collider 1 Answer

Physics.Raycast not working as expected 1 Answer

[c#] While loop not exiting, I have no idea why 2 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