Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Shessen · Apr 30, 2016 at 06:34 PM · networkingphysics2dbooleanlayermasklinecast

How can I check a Linecast across multiple layers, and assign the result to a single bool?

So, I want to have a player that's registered as "grounded"(bool) when he stands on the "Floor" Layer or "GrateFloor" layer.

I use a Linecast to check a hit on the "Floor" layer, and set grounded to true or false based on that. Here's the code:

// The player is grounded if a linecast to the groundcheck position hits anything on the floor layer. grounded = Physics2D.Linecast(player.position, groundCheck.position, 1 << LayerMask.NameToLayer("Floor"));

And it works. But what I need is to have the linecast also check the "GrateFloor" layer, but I'm not sure how. If I try this:

grounded = Physics2D.Linecast(player.position, groundCheck.position, 1 << LayerMask.NameToLayer("Floor") || LayerMask.NameToLayer("GrateFloor"));

or this:

grounded = Physics2D.Linecast(player.position, groundCheck.position, 1 << LayerMask.NameToLayer("Floor") || 1 << LayerMask.NameToLayer("GrateFloor"));

I get errors. If I try this:

grounded = Physics2D.Linecast(player.position, groundCheck.position, 1 << LayerMask.NameToLayer("Floor")); grounded = Physics2D.Linecast(player.position, groundCheck.position, 1 << LayerMask.NameToLayer("GrateFloor"))

one overrides the other.

What's the best way to check more than one Layer, and assign the result to a single variable?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by jgodfrey · Apr 30, 2016 at 07:02 PM

Check the "Combining LayerMasks" section in the accepted answer here:

http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

Comment
Add comment · Show 3 · Share
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 jgodfrey · Apr 30, 2016 at 07:34 PM 0
Share

So, specifically, that'd be something like this:

 int mask1 = 1 << Layer$$anonymous$$ask.NameToLayer("Floor");
 int mask2 = 1 << Layer$$anonymous$$ask.NameToLayer("GrateFloor");
 int combined$$anonymous$$ask = mask1 | mask2;
 grounded = Physics2D.Linecast(player.position, groundCheck.position, combined$$anonymous$$ask);
avatar image Shessen · Apr 30, 2016 at 08:12 PM 0
Share

That worked, thanks.

avatar image murchu27 · Jan 10, 2019 at 12:05 PM 0
Share

Upvoting for translating the Javascript used in the referenced answer to C# :P

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

71 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 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 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

Linecast not working after Instantiation of a particular object 0 Answers

Calling Bool in Network Command Not Working 0 Answers

Network enemy spotting 0 Answers

Physics2D.OverlapBox shows inconsistent behaviour 0 Answers

Unet Syncing Flashlights 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