hello all,I’m learning the Raycast function.
I will create a scene:These objects(box01,box02,box03_noColliedr) in the object(ColliderBase),I just want to access to objects(box01,box02,box03_noCollider).But I set the LayerMask into any value,I don’t access to those objects(box01,box02,box03_noCollider):shock:,Why is this?
using UnityEngine;
using System.Collections;
public class Ray02 : MonoBehaviour
{
GameObject Text01;
Ray sy01;
RaycastHit sy_Hit01;
public int MyLayerMask = 0;
// Use this for initialization
void Start()
{
Text01 = GameObject.Find("Text01");
}
void Update()
{
sy01 = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(sy01, out sy_Hit01, 1000f, MyLayerMask) Input.GetMouseButtonDown(0))
{
Text01.guiText.text = sy_Hit01.transform.name.ToString();
}
}
}
NameToLayer() returns the layer index; ‘1 << …’ converts that to a bit mask, turning on the bit associated with that layer. To select more than one layer to raycast against, use
but,If I use the “MyLayerMask = (1 << LayerMask.NameToLayer(“My Layer Name”)) (1 << LayerMask.NameToLayer(“My Other Layer Name”));”,unity will prompt error: error CS0019: Operator ' cannot be applied to operands of type int’ and `int’
using UnityEngine;
using System.Collections;
public class Ray02 : MonoBehaviour
{
GameObject Text01;
Ray sy01;
RaycastHit sy_Hit01;
public string MyLayerMaskName = "Anchor";
public string OtherLayerMaskName = "NoAnchor";
public int MyLayerMask = 1;
// Use this for initialization
void Start()
{
Text01 = GameObject.Find("Text01");
MyLayerMask = (1 << LayerMask.NameToLayer(MyLayerMaskName)) (1 << LayerMask.NameToLayer(OtherLayerMaskName));
}
void Update()
{
MyLayerMask = (1 << LayerMask.NameToLayer(MyLayerMaskName)) (1 << LayerMask.NameToLayer(OtherLayerMaskName));
sy01 = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(sy01, out sy_Hit01, 1000f,MyLayerMask ) Input.GetMouseButtonDown(0))
{
Text01.guiText.text = sy_Hit01.transform.name.ToString();
}
}
}
is true if the left side and the right side are true
is the bit-operator AND.
Also I think you need to use Vertical Bar ( | ) instead - bitwise OR. That adds two layer masks together, while bitwise-AND only tells you which layers those masks have in common.
You can also declare a LayerMask variable that can be easily edited in the Inspector.