- Home /
C# LayerMask as a static method parameter?
Can I pass a LayerMask as a parameter into a static function?
public static Transform qmTransform_PlayerToMob(Vector3 startPos, float maxDist, LayerMask layermask) {
Transform target = null;
RaycastHit hitInfo = new RaycastHit ();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Change ray origin accomodating zoom/orbit Camera
ray.origin = startPos;
bool hit = Physics.Raycast (ray, out hitInfo, maxDist, layermask);
if (hit) {
target = hitInfo.transform;
// This is basically debug, remove later
QM_ClientGUI.PLogMessages ("Hit: " + target.name);
}
return target;
}
I found I was coding something like that in several classes and thought I would try a static version using parameters. However, when I call this from another class that has layermask defined and setup properly in Inspector with this:
QM_Utils.qmTransform_PlayerToMob (myTransform.position, 100, layerMask);
I get a Transform returned that is not in the caller's configured layerMask.
That should work - surely something weird with the way you have the mask set up?
I think your issue is co$$anonymous$$g from assigning an int directly to layermask. You should use a 1<< myInt to get the right mask before passing it in to your static function.
You wrote that you get a Transform not having the configured layer mask, but Raycast uses this parameter to ignore objects, not to include them. If you want to get objects belonging to specific layer, then use ~layermask ins$$anonymous$$d.
Arkane: well, yes, layer$$anonymous$$ask is only needed if you want to Ignore certain layers. But the way it works is by saying what you want to hit. It's confusing since the default is not 0000000. If you print Physics.kDefaultRaycastLayers
, it's -5, which is 1111...111011. Layer 2 is the IgnoreRaycast layer. So you can see 1=hit this, 0=skip this.
In other words, ~layer$$anonymous$$ask
is for hitting every except that one layer. layer$$anonymous$$ask
hits only that layer.
I've just deleted my invalid comment after further tests. It looks like related line in my code was working by coincidence only. Owen - thank you for clarification!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
ArgumentException: NameToLayer can only be called from the main thread 1 Answer
C# Going Static or Going OOP 3 Answers
Can't Encode MD5 2 Answers