Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 t_w · Sep 07, 2015 at 11:00 AM · 2d gamescript.layerlayermask

A line of code, I do not understand. 1<

grounded=Physics2D.Linecast(transform.position, groundCheck.position, 1<<LayerMask.NameToLayer("Ground"));

I know linescast, but the last codes 1<<LayerMask.NameToLayer("Ground")

What is the meaning, how to use it?

It is from this tutorial 2d platform game

Thanks!

Comment
Add comment · Show 1
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 CaptainKirby · Sep 07, 2015 at 11:05 AM 0
Share

It merely states that the linecast only checks on the layer called Ground.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by YoungDeveloper · Sep 07, 2015 at 11:24 AM

It's layer specifying using bit shifting. It's very well explained here.

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

Answer by Suddoha · Sep 07, 2015 at 11:32 AM

It's a shifting operation. LayerMasks use the bits of an integer (4 bytes = 32 bits, thus you can have 32 layers) to store multiple layers in one integer efficiently. That means, you can combine any layers that you like and only need 4 bytes, i.e. a simple integer. LayerMasks.NameToLayer returns the index of a given layerName, for example you've got a layer called "myLayer" at the 9th position, which is, according to 0-based counting, the index 8.

That in turn means, if you want to use this layer in a LayerMask, you have to set the 9th bit of an integer.

Imagine you've got an integer, which is currently 0, that represents your list of layers (0 not set, 1 set)

0000 0000 0000 0000 0000 0000 0000 0000

You now have "myLayer" in the 9th slot in the layer list. According to that, you want the following LayerMask

0000 0000 0000 0000 0000 0001 0000 0000

As stated above, the method LayerMask.NameToLayer(...) returns the index (remember, it's 8) so you can now do the following. Start with 1 written in binary:

0000 0000 0000 0000 0000 0000 0000 0001

and shift it

once: 0000 0000 0000 0000 0000 0000 0000 0010

twice: 0000 0000 0000 0000 0000 0000 0000 0100

[...]

8 times: 0000 0000 0000 0000 0000 0001 0000 0000

Voila, you have the appropriate LayerMask for your layer.

Comment
Add comment · Show 2 · 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 t_w · Sep 07, 2015 at 11:40 AM 0
Share

what does*Layer$$anonymous$$ask.NameToLayer("Ground")* return?

an int?

avatar image maccabbe · Sep 07, 2015 at 12:59 PM 0
Share

Yes, Layer$$anonymous$$ask.NameToLayer "returns the layer index"

http://docs.unity3d.com/ScriptReference/Layer$$anonymous$$ask.NameToLayer.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Gameobject Gravity 2D Script 0 Answers

How to get the layer id from a layer mask? 0 Answers

[Question] Unity C# Set a Player Class and refence issue 0 Answers

Identifying 3 GameObjects in contact with each other,Identifying and destroying 3 Game Objects with the same LayerName 0 Answers

Allow raycast to skip an object with rigidbody 0 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