Why does my raycast detect every layermask?

``using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlacementCheking : MonoBehaviour
{
BuildingScript BuildingScript;
// Start is called before the first frame update
void Start()
{
BuildingScript = GetComponent();
}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(transform.position, transform.forward, 00110))
{
print(“Good”);
}

}
}
}

Okay so this is how far i come to day, But even if i use a layer mask it still detects all the others im using layer mask 6 or in binary 00110 which is the one i want, but the raycast still detects every layermasks?

It looks like you didn’t end up putting the code correctly inside of the code tags.

When doing a binary number, you want to put 0x before the number so the compiler knows you’re inputting a binary number. It would look like this.

Physics.Raycast(transform.position, transform.forward, 0x00110)

Alternatively, it might be easier to just have a Serialized LayerMask object, as the Inspector has a neat thing for setting LayerMasks.

Unity now has a LayerMask class. But if you want to make them by hand, the common way is something like 1<<8 | 1<<6 | 1<<3, which selects layers 8, 6 and 3 (the order doesn’t matter, 1<<3|1<<6|1<<8 is the same). You should be able to look up plenty of older Unity examples using that.

Just a quick question cant i just use the no ray cast layer on my prefab? I’m trying to make a building system and I don’t want to be able to place a object on another object. And with the no ray cast layer it is not possible to place things on that object?

So, the only thing the LayerMask will do is ignore the object when shooting a Raycast, essentially treating it as if it doesn’t exist. If you don’t want something to be built on, you should use the Raycast to check what kind of object it is, and go from there.

On another note, I re-read your original post, and if you wanted a LayerMask representing the 6th layer, it would be 0x100000. Each bit represents a layer, and whether or not it is on or off. Doing what Owen-Reynolds suggested would be easier than trying to parse out the number of 0’s though, 1<<6 is much cleaner looking.

I also always forget whether the LayerMask input is the Layers to ignore, or the Layers to hit, so in the event you had to inverse it, you would have to use a NOT operator to flip it.

But in the end, the path of least resistance is to just use the LayerMask class as a SerializeField object.

1 Like

Okay so I hopefully added this right

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



[B]public class PlacementCheking : MonoBehaviour[/B]
{
    public bool canPlace;
    BuildingScript BuildingScript;
    [SerializeField] LayerMask layerMask;
    // Start is called before the first frame update
    void Start()
    {
        BuildingScript = GetComponent<BuildingScript>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Debug.DrawRay(transform.position, Vector3.forward, Color.black, 5f);
            if(Physics.Raycast(transform.position, transform.forward, layerMask))
            {
                canPlace = false;
            }
        } 
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[B]public class BuildingScript : MonoBehaviour[/B]
{
    public Transform Cube; // Temp building
    public LayerMask groundLayer;
    public bool canPlace;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        PlacementSystem();
    }

    public void PlacementSystem()
    {
        if(Input.GetMouseButtonDown(0) && canPlace)
       {
            if(Physics.Raycast(transform.position, transform.forward, out var hit, groundLayer))
            {
               

                Instantiate(Cube, hit.point, Quaternion.identity);
            }
       }
    }

}

This are my two scripts, After this i just need to set the canPlace to false in the chekplacement script?