- Home /
Question by
noise256 · Apr 15, 2013 at 02:28 PM ·
raycastreturn value
How to return null if raycast didn't hit anything?
Hello,
I'm trying to write a small class that has static methods for performing raycasting but I'm not sure how to get the methods to return null if the raycast didn't hit anything. Perhaps my thinking is too Java orientated but this is want I want to do. I understand the usual process when raycasting would be to do:
if (Physics.Raycast(ray, out hit) {
//do stuff for hit
}
else {
//do stuff for non hit
}
However I want to write a class that has static methods including the following:
public static RaycastHit raycastFloor(Vector3 screenPoint) {
int layerMask = 1 << LayerMask.NameToLayer("Floor");
Ray ray = Camera.main.ScreenPointToRay(screenPoint);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, layerMask)) {
return hit;
}
else {
return null; //What to do here?
}
}
Comment
Your answer

Follow this Question
Related Questions
Using Trigonometry for Collision Detection 0 Answers
Why does this script I got from the Unity Document site does not work? 1 Answer
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer
How to obtain third person accuracy 0 Answers
How to send a raycast to an object to give me ammo? 0 Answers