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 El Maxo · Jan 16, 2015 at 02:52 PM · raycastlayermask

Raycasting layermask issue

Hi

I Have been reading thre the documention on this subject and have come across quite stummeped. I have tryed to make sure that my code only works when the ray goes agesnt a object with the layer name of "Info". The issue is that the code works on everything still. The code that is relivent is as follows.

 public LayerMask info; //sets layermask in inspector

 
 if (Physics.Raycast (ray, out hitInfo, info.value)) { //sets the layormask on the raycast.
Comment
Add comment · Show 8
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 stulleman · Jan 16, 2015 at 02:59 PM 0
Share

How do you initialize your Layer$$anonymous$$ask?

Try: Physics.Raycast ( ray, out hitInfo, 1 << Layer$$anonymous$$ask.NameToLayer("The name of the layer"));

avatar image El Maxo · Jan 16, 2015 at 03:18 PM 0
Share

I thought I had initialised witht the line :

 public Layer$$anonymous$$ask info; //sets layermask in inspector
avatar image stulleman · Jan 16, 2015 at 03:24 PM 0
Share

No, you would have to put something in info. $$anonymous$$aybe you did in the inspector? But anyway, try the way I showed in the comment above.

 if(Physics.Raycast ( ray, out hitInfo, 1 << Layer$$anonymous$$ask.NameToLayer ("The name of the layer")) {
    ...
 }
avatar image El Maxo · Jan 16, 2015 at 03:30 PM 0
Share

yes I did in the inspector, I have tryed your way as well to no avail. Quick Question what does the "1 << " bit do

avatar image stulleman · Jan 16, 2015 at 03:38 PM 0
Share

A Layer$$anonymous$$ask is like the name says an integer mask. So 1 << 5 would look like this : 00010000. So 1 << shifts a bit 5 bits to the left (in this example).

By the way are you using 2D or 3D? And are you sure you used the correct name? (It's case sensitive)

Show more comments

1 Reply

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

Answer by stulleman · Jan 16, 2015 at 04:04 PM

EDIT: I think I found your error. You are missing a parameter. You are using the length of the ray as LayerMask. Try adding a length.

Okay I have tested this and it's working.

I made a Scene with 3 cubes. 2 in dafult Layer and 1 in "ChangeColor" Layer. Attach the following code to the camera. Try to understand it and play with it.

 using UnityEngine;
 using System.Collections;
 
 public class ChangeColor : MonoBehaviour {
 
     
     void Update () {
         
         RaycastHit hitInfo;
 
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 1000, 1 << LayerMask.NameToLayer("ChangeColor"))){
             hitInfo.transform.renderer.material.color = Color.green;
         }
     }
 }
 
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 Owen-Reynolds · Jan 16, 2015 at 05:30 PM 0
Share

Ha! Common mistake. A trick is, when you enter the stuff in parens, look at the variable names in the pop-up. You can delete and retype the comma to get them back.

It's common in overloaded functions (where there are several versions) to have things like this -- where all the versions with layer$$anonymous$$ask also require distance.

Seems like nameToLayer complicates things. When you made the layer$$anonymous$$ask, you had to pick a slot number for it, anyway. $$anonymous$$aybe it was 9. So can use just 1<<9 (which I think is also in more examples.) (But if nameToLayer works, don't change it.)

avatar image stulleman · Jan 16, 2015 at 05:35 PM 0
Share

Yeah it depends on the IDE you are using but that's how I noticed it when I scripted it :P

What nameToLayer does is simple give you the number of the Layer. So Basically there is 1 << 9 if the required Layer is number 9. So you don't have to remember the slot number :P

avatar image Owen-Reynolds · Jan 16, 2015 at 11:34 PM 0
Share

$$anonymous$$y point about nameToLayer is that often things like this have some "extra magic" and you just have to use them. But not in this case.

By that point you've seen the layer is just a number, and even had to pick the number. Then you see nameToLayer and think "but wait, so the layer isn't that number? Then why did I have to do that?"

So, I wanted to say that, yes, layers are just numbers. nameToLayer is just a pretty way to get that number. On the plus side, very easy to read later. On the maybe $$anonymous$$us side, that's a lot to type. Esp. since you will probably only ever use 3 layers.

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

26 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

Related Questions

How to make my raycast also hit deactive objects WITHOUT showing the object? 1 Answer

How can I have a raycast ignore a layer completely? 2 Answers

A question about "Physics.Raycast()" 1 Answer

Raycasting with LayerMask gives me a bce0023 1 Answer

Layermask doesn't seem to work 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