- Home /
Four different Raycast functions?
Hi all. I'm reading and trying to make sense of the raycast documentation:
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Like all other docs, it mentions the function name, parameters, returns and description.
And then it just goes straight on and lists another three entire sets of parameters, returns, and descriptions, without naming any new function. I'm trying to gel this with what i generally know about object oriented programming, and it just feels wrong.
Like the first one takes origin, direction, distance, and layermask But then the second function takes Origin, direction, distance, hitInfo, and layermask.
How is that possible ? how does it not just throw a compiler error because you passed it a RaycastHit when it was expecting a LayerMask?
Can someone please explain this like i'm a student?
Answer by Em3rgency · Oct 19, 2014 at 02:37 PM
http://www.tutorialspoint.com/csharp/csharp_polymorphism.htm
This is a feature of c# (and many other coding languages). In short: You can define the same function with different parameters, and as long as they're different, the compiler will know which one you mean, based on the arguments you pass it. For example:
void poly(int a)
{
print("I got an int.");
}
void poly(double a)
{
print("I got a floating point.");
}
<...>
poly(1); //result: "I got an int";
poly(1.525); //result: "I got a floating point."
And it will let you know, when it can't find a suitable override.
poly("hi"); //results in an error
Your answer
Follow this Question
Related Questions
Can you 'layer mask' all physics? (Solved.) 1 Answer
2D Raycast Layer Mask not working 1 Answer
Raycast problems in my AI 1 Answer
Raycast 2D with origin inside the collider 1 Answer
Physics Raycast LayerMask problem? 3 Answers