- Home /
Raycast and RaycastHit failure
I have a raycast which is meant to check that an object with the tag "Snake" is under the cursor. I've been trying to do some research on this problem, but none of it is making sense or it doesn't solve the problem. Here's the code:
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit,Mathf.Infinity,LayerMask.NameToLayer("Default"))){
Debug.Log("Raycast works");
if(hit.transform.gameObject.CompareTag("Snake")){
Debug.Log("RaycastHit comparison works");
}
}
Neither debug log displays, the objects with the tag have rigidbodies, and I am using #pragma strict. What could be the problem?
That works! For some reason, the function didn't work without that before, but it works perfectly without it. Thank you!
Answer by whydoidoit · Jun 18, 2012 at 07:28 PM
Have you tried it without the layer mask to see if that works?
Answer by Wolfram · Jun 18, 2012 at 07:48 PM
You need to use 1<<LayerMask.NameToLayer("Default")
instead. I think LayerMask.NameToLayer("Default").value
will do, too.
EDIT: Nope, the .value version works only on LayerMasks, but LayerMask.NameToLayer returns an int, not a LayerMask. Whoever wrote that API wanted to do something evil.
Your answer
Follow this Question
Related Questions
Ray Casting - Trigger function 1 Answer
Play ParticleSystem on RayCastHit Object 1 Answer
Using a ray to make drop script 1 Answer
See what object I hit in raycast 0 Answers
How do I ask if RaycastHit returns null? 3 Answers