- Home /
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.
How do you initialize your Layer$$anonymous$$ask?
Try: Physics.Raycast ( ray, out hitInfo, 1 << Layer$$anonymous$$ask.NameToLayer("The name of the layer"));
I thought I had initialised witht the line :
public Layer$$anonymous$$ask info; //sets layermask in inspector
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")) {
...
}
yes I did in the inspector, I have tryed your way as well to no avail. Quick Question what does the "1 << " bit do
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)
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;
}
}
}
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.)
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
$$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.