Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by getyour411 · Mar 04, 2014 at 06:00 AM · c#staticlayermaskparameter

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.

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 04, 2014 at 07:15 AM 0
Share

That should work - surely something weird with the way you have the mask set up?

avatar image nesis · Mar 04, 2014 at 07:30 AM 2
Share

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.

avatar image ArkaneX · Mar 04, 2014 at 11:57 AM 0
Share

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.

avatar image Owen-Reynolds · Mar 04, 2014 at 04:04 PM 1
Share

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.

avatar image ArkaneX · Mar 04, 2014 at 07:43 PM 0
Share

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!

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges