- Home /
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);
}
}
}
}
Answer by KurtIngamells495 · Jul 01, 2013 at 01:54 PM
Problem resolved. Player layer was set to Ignore Raycast, lol
Just be aware that it isn't always viable to put the player on that layer. Sometimes you want certain raycasts to hit the player. Remember that if later you are trying to use raycasts and it isn't working.
Yes, thank you for letting me know but I can't think of any time I will be using raycasts that need to hit the player, plus this is really only a test game because I'm new to coding in c# and trying to learn how to generate a world terrain out of blocks and be able to edit it just like in $$anonymous$$C and Starforge, and then to save and load that data in the smallest possible file size. That is the purpose of this project, and in the actual Game once I start making it I doubt I will be using raycast against the player :)
That's fine. It was mostly an FYI for future consideration. Good luck!
$$anonymous$$urt, you should TIC$$anonymous$$ one of the useful answers. On the OP can close out a question. Also, you get "points" so that next time you can ask questions without moderation. Cheers
Thanks, sorry I am so tired it's unbelievable plus this forum isn't the easiest to use, I must say the layout confuses me a lot :L
Answer by amphoterik · Jul 01, 2013 at 01:34 PM
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: http://answers.unity3d.com/questions/416919/making-raycast-ignore-multiple-layers.html
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
}
http://answers.unity3d.com/questions/409360/raycasthit-behind-object.html
Answer by Cence99 · Jul 01, 2013 at 01:00 PM
//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) :)
Thank you so much, that has solved the problem of the player object getting deleted, and it turns out the prefab containing the player controllers didn't have the Player tag applied for some reason, even though normally it does by default. How could I make it so the raycast continues through the player though because now I can't destroy the block underneath me it just does nothing :(
the answer is at hand, Physics Layers ...
http://answers.unity3d.com/questions/382342/npcs-collide-with-collider-but-player-does-not.html