int layM = 1 << 16;
(…)
if (Physics.Raycast(transform.position, Vector3.left, out hit, layM)){
My objects which I dont want to be seen by Raycast are marked as user layer 17, but program still sees it. What should I do?
print(layM); //is 65536
Your current code is being interpreted as “only detect collisions with layer 16”.
You need to use the binary not operator to reverse that so your code will be interpreted as “detect collisions with all layers except layer 16”.
Here’s the code:
int layM = 1 << 16;
// the ~ is the binary not operator
layM = ~layM
but this causes:
Scene::raycastClosestShape: The maximum distance must be greater than zero!
UnityEngine.Physics:Raycast(Vector3, Vector3, RaycastHit, Single)
AI:Update() (at Assets/Scripts/AI.cs:32)
~layM (1 << 16) = -131073
You have your parameters mixed up, you need to specify the length of your ray cast before you’re layer mask.
For example:
if (Physics.Raycast(transform.position, Vector3.left, out hit, Mathf.Infinity, layM)){
Mathf.Infinity is the length of the ray cast in this example.