- Home /
RaycastHit returns object without collider, in wrong layer
I have a Game Object with a script that has a function which raycasts four times around it to assess the Objects around it (Essentially, I have box colliders set up in a grid that check each other in a function). While these raycasts usually go through fine, there is a problem where the raycast keeps picking up certain objects (Walls, as planes in between the colliders) which have mesh collider turned off, and the raycast is set to pick up only layer 10, while these walls are on layer 12. Why is this happening, and how do I fix it?
With seemingly irrelated code removed, this is what I'm doing. print(colliderMapTile) returns an instantiated Prefab with it's collider turned off with it's layer set to 12.
var layermask = 1 << 10;
function updateSurroundingRooms(){
var cardinalDirection = new Vector3[4];
cardinalDirection[0] = transform.TransformDirection (Vector3.forward);
cardinalDirection[1] = transform.TransformDirection (Vector3.right);
cardinalDirection[2] = transform.TransformDirection (Vector3.back);
cardinalDirection[3] = transform.TransformDirection (Vector3.left);
for(var i : int = 0; i < 4; i++){
var hit : RaycastHit;
var colliderMapTile : GameObject;
if (Physics.Raycast (transform.position, cardinalDirection[i], hit, layermask)) {
colliderMapTile = hit.collider.gameObject;
print(colliderMapTile);
colliderMapTile.GetComponent("MapTileColliderBehaviour").roomChange();
}
}
}
Not sure this has a impact on your issue, but layers are numbered from 0. So 1 << 10 will find the 11th layer in your list.
I'm pretty sure 1 << 10 will enable the 10th layer, and all the documentation I've read supports that. But without any other choice, I tried that. It didn't break what was already working. It changed absolutely nothing. I don't think my raycast is even using the Layer$$anonymous$$ask.
Ha. It isn't. I formatted the raycast wrong, and forgot to slip a '1' in before the layer mask for the raycast range. Actually, now that it isn't returning the wall objects, I tried playing with the layermask. It still works regardless of the value of the layermask, and I still wonder why the raycast was picking up an object without a collider.
Layers are are zero based, If you pull down the Layer dropdown and select 'Edit layers', you will see "User Layer 10." That is the layer that 1 << 10 enables. But the layers start with "Element 0," so if you count, that is the 11th layer. If you do 1 << 0 (or just 1), you get the layer called "Element 0";
Answer by robhuhn · Aug 08, 2013 at 06:31 AM
There is no overload for
Physics.Raycast (origin: Vector3, direction: Vector3, hitInfo: RaycastHit, layerMask: int)
In your code it takes the bitmask as the distance parameter.
Replace it with
Physics.Raycast (transform.position, cardinalDirection[i], hit, yourDistance, layermask)
@edit
btw. if you use var mask : LayerMask on a component you should see the layermask dropdown in the inspector to configure layermasks easily. Anyway that's how it's done in c# public LayerMask mask and I guess it also works in us/js.
Your answer
Follow this Question
Related Questions
How can I let the ray to the edge of the capsule? 1 Answer
Even-odd test with raycasting 1 Answer
OnCollisionEnter2D is not working 0 Answers
raycasts only working with IsTrigger colliders? 1 Answer
SInce when?!?!? (Raycast issue) 2 Answers