Raycast ignore player

I have a script in C# which I have written that allows a player to create and destroy blocks just like in Minecraft. I have NEVER used raycast before and am not very good at it. The problem is that if you look down (I adapted the mouse look script to allow you to look directly up or down - 90 degrees instead of 60), and you right click to destroy the block below you, it destroys the player controller. I’m using the standard 1st person controller.
This is the code… all blocks have a “Block” tag, maybe we could use this to our advantage so it only destroys blocks…

using UnityEngine;
using System.Collections;
public class BuildScript : MonoBehaviour
{	
	//Create a variable for the raycast system to allow it to 'hit' prefabs
	RaycastHit hit;
	
	//Blocks
	public Transform emptyPrefab;
	public Transform grassPrefab;
	public Transform cardboardPrefab;
	public Transform slatePrefab;
	public Transform leatherPrefab;
	public Transform brickPrefab;
	public Transform stonePrefab;
	public Transform dirtPrefab;
	public Transform bedrockPrefab;
	
	//Private variables
	private Transform selectedPrefab;
	private float selectedBlockID;
	
	void Start ()
	{
		//Set the default prefab
		selectedPrefab = grassPrefab;
	}
	
	void Update ()
	{
		//Allow the player to change the selected ID by using the 1-0 keys
		if (Input.GetKey ("1")){selectedBlockID = 1;}
		if (Input.GetKey ("2")){selectedBlockID = 2;}
		if (Input.GetKey ("3")){selectedBlockID = 3;}
		if (Input.GetKey ("4")){selectedBlockID = 4;}
		if (Input.GetKey ("5")){selectedBlockID = 5;}
		if (Input.GetKey ("6")){selectedBlockID = 6;}
		if (Input.GetKey ("7")){selectedBlockID = 7;}
		if (Input.GetKey ("8")){selectedBlockID = 8;}
		if (Input.GetKey ("9")){selectedBlockID = 9;}
		if (Input.GetKey ("0")){selectedBlockID = 10;}
		
		//Make sure that the correct block for the ID is selected	
		if(selectedBlockID == 1){selectedPrefab = grassPrefab;}
		if(selectedBlockID == 2){selectedPrefab = cardboardPrefab;}		
		if(selectedBlockID == 3){selectedPrefab = slatePrefab;}	
		if(selectedBlockID == 4){selectedPrefab = leatherPrefab;}	
		if(selectedBlockID == 5){selectedPrefab = brickPrefab;}
		if(selectedBlockID == 6){selectedPrefab = stonePrefab;}
		if(selectedBlockID == 7){selectedPrefab = dirtPrefab;}
		if(selectedBlockID == 8){selectedPrefab = bedrockPrefab;}
		if(selectedBlockID == 9){selectedPrefab = emptyPrefab;}
		if(selectedBlockID == 10){selectedPrefab = emptyPrefab;}
		
		//Fire the raycast system from the center of the screen in the direction the camera is looking
		Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
		
		//Align newly created blocks to a grid
		Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
		
		//Check what prefab the raycast has hit and allow the player to create/destroy in that position
		if(Physics.Raycast(ray, out hit))
		{			
			//Left click
			if(Input.GetMouseButtonDown(0))
			{
				//Destroy the block at that position
				Destroy(hit.collider.gameObject);
			}
			
			//Right click
			if(Input.GetMouseButtonDown(1))
			{
				//Create a block at that position
				Instantiate(selectedPrefab, G, Quaternion.identity);
			}
		}
	}
}

Set a layermask for your raycast. This will allow you to determine which layers are hit by the raycast and which aren’t. This will also solve the issue of the ray stopping at the player when you want it to continue.

More information can be found here: Making raycast ignore multiple layers - Questions & Answers - Unity Discussions

This answer is provided by Alucardj:
It is much easier to declare a variable that is typecast as LayerMask, then in the Inspector, simply tick the layers that you wish to be detected by the raycast. As a test, create a new script, at the top put

var myLayerMask : LayerMask;

Now save this script, go back to Unity and attach the script to an empty gameObject or the camera (it doesn’t matter, this is just a test). Now look in the inspector, click on the drop-down menu then tick the layers you wish to be acknowledged. This is much easier than trying to implement the bitshift method in the documentation. Now you’ve seen how it works, simply use :

var myLayerMask : LayerMask;
 
// in a function
if ( Physics.Raycast(transform.position, transform.forward, Hit, Range, myLayerMask) )
{
// Do Stuff
}

//Left click
if(Input.GetMouseButtonDown(0) && hit.collider.tag != “Player”)
{
//Destroy the block at that position
Destroy(hit.collider.gameObject);
}

Just check if the ray hasn’t hit the Player (you need to tag the Player as “Player” or something else) :slight_smile:

Problem resolved. Player layer was set to Ignore Raycast, lol