- Home /
Raycast hitting the incorrect layer
Ok so I have to imagine that this is a problem with my code. I'm fairly new to raycasts, but the research I have done and the experiments I've done have shown me that Raycasts really aren't all that hard to use. Well, maybe it sensed me thinking that and decided to give me a hard time. Here is the screenshot,
Now here is my code (Explanation of problem is down below):
using UnityEngine;
using System.Collections;
public class UnitSelectionTest : MonoBehaviour {
RaycastHit hit;
public static LayerMask terrainLayerMask = 1 << 8;
public static LayerMask unitLayerMask = 1 << 10;
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, out hit, Mathf.Infinity, terrainLayerMask)) {
if(hit.collider.tag == "Terrain") {
print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
Debug.DrawLine(ray.origin, hit.point, Color.red, 60.0f);
}
}
if(Physics.Raycast (ray, out hit, Mathf.Infinity, unitLayerMask)) {
if(hit.collider.tag == "Selectable") {
print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
Debug.DrawLine(ray.origin, hit.point, Color.red, 60.0f);
}
}
}
}
}
Ok, so here's the problem. The above screenshot is showcasing, for good measure, a series of Raycasts being shot on all sides of the cube, minus the bottom. The raycasts are all detecting that they are hitting the terrain layer instead of the layer that the cube is in. Now I'm about 80% certain that this is an error with my code. But get this, when I move the cube high enough so that the terrain is no longer being viewed, the ray cast detects that it is hitting the cube. But as soon as I put it back down, it thinks that it's hitting the terrain again. So what do you recommend I do? I've done a lot of searching for a problem such as this but have found none that yielded working results. Is it my syntax? Do I have a block of code incorrectly placed? Why is it hitting the terrain when it's visible, but as soon as the terrain is taken out of the picture, or I completely comment out the "if-statement" checking to see if the raycast hit the terrain, is it working properly? My brain is fried and I'm out of ideas. Any and all help is very much appreciated.
Answer by BBrown4 · Feb 13, 2013 at 06:21 AM
Ok, well I seem to have fixed it.
void Update () {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, out hit, Mathf.Infinity, unitLayerMask)) {
if(hit.collider.tag == "Selectable") {
print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
Debug.DrawLine(ray.origin, hit.point, Color.red, 10.0f);
}
}
else if(Physics.Raycast (ray, out hit, Mathf.Infinity, terrainLayerMask)) {
if(hit.collider.tag == "Terrain") {
print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
Debug.DrawLine(ray.origin, hit.point, Color.red, 10.0f);
}
}
}
}
I guess it just takes a little playing around with. I figured maybe it was checking for two different raycasts in the same frame. So I probably should have done this from the start, but I just added the "else-if" statement instead of the two "if" statements so that way it would say, "Ok if it's not one then it must be the other". Makes sense to me, not sure if I made sense to anyone else haha but thanks for the help! :)
I also changed the the layermasks as well.
public static Layer$$anonymous$$ask terrainLayer$$anonymous$$ask = 1 << 8;
public static Layer$$anonymous$$ask unitLayer$$anonymous$$ask = ~terrainLayer$$anonymous$$ask;
Answer by iwaldrop · Feb 13, 2013 at 02:44 AM
I assume you mean to be checking the unitLayerMask in your second Raycast? Just to make sure your raycasting is working correctly, you can remove the layermask entirely. There is a cool Layer for objects that you don't want to include in raycasting called "IgnoreRaycast". Find it in the inspector.
Another element to this is that you probably don't need to do a separate raycast for each layer you want to hit. Just do one raycast, ignore some layers via the mask if you want, and query the RaycastHit for what it hit. You're definitely on the right track!
Right, I'm trying to get the raycast to ignore the terrain layer mask. The reason for the multiple casts is just to show that at no matter what angle they come from, they still seem to be hitting the terrain layer. For some reason, when the camera is looking at the terrain, the raycasts will always hit the terrain for some reason, but when the cube is high enough to where the camera is not pointing at the terrain, it hit detects that it is hitting the cube. Strange, I wonder if this has to do with what the camera is looking at, although, the ray variable is set to fire the raycast on the mouse position, shouldn't that be enough? Oh and I just realized I had a type in my code. In the second if statement it's supposed to look for the unitLayer$$anonymous$$ask not the terrainLayer$$anonymous$$ask again. I fixed it. It's the same issue but just wanted to let you know that's not the cause.
Update: Well, I have been playing around with it and now I've gotten it to properly hit the cube, but the only issue now is that the raycast is going THROUGH the cube. So essentially it's hitting both. Is there a way to get the raycast to not go through the cube? Because if the raycast hits the cube, but goes through and hits the terrain also it would cause complications in my code.