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
0
Question by smtabatabaie · Oct 16, 2011 at 03:30 PM · androidjavascriptraycasttouchlayermask

raycasting and layermask question

hi , I wanted to be able to rotate an object in my android app with touch , I found this very useful tutorial on how to do this : http://revelopment.co.uk/tutorials/unitytutorials/73-howtorotateanobjectbytouch
but I don't get some parts of it's script . first I don't get why it has used raycasting? I mean it works fine with commenting out the raycast condition , and I don't get the '<<' on layermasks , I haven't seen such operator on other languages .
I'll be appreciated if someone can help me with this

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

2 Replies

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

Answer by superpig · Oct 16, 2011 at 03:40 PM

Raycasting is used to make sure that the player has actually touched the object. If you only have one object on the screen, and you want them to be able to press anywhere to rotate it, then you might not need it, but if you've got a bunch of objects on the screen then you'd need to know which one they pressed on, so you know which one to rotate.

The '<<' operator is 'shift left.' "X << Y" takes the binary representation of X, and puts Y zeros on the end of it - so '1 << 4' results in the binary expression '10000', or 16 in decimal.

Layer masks provide an easy way to store 'on/off' values for multiple layers as a single number. If bit 0 is on, then it means layer 0 ("Default") is included in the mask. If bit 1 is on, then it means layer 1 ("Ignore Raycast") is included. And so on.

Shifting is helpful here because the layer mask that includes only layer X can be calculated as "1 << X" - and multiple layer masks like that can be combined using the bitwise-OR operator, |. So the statement at the top of the code is setting up a layer mask that includes layers 2 and 8.

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 smtabatabaie · Oct 16, 2011 at 06:53 PM 0
Share

thanks , as far as I understood the script uses 2 , 8 layers and then inverting the layer mask in the Start() function to disable the objects on those layers . but why the script only works on my device when I comment out the raycast condition?

avatar image smtabatabaie · Oct 16, 2011 at 08:08 PM 0
Share

even when I delete the layermask parameter from Physics.Raycast function , it doesn't work ;(

avatar image syclamoth · Oct 16, 2011 at 11:38 PM 0
Share

Oh! If it uses Input.GetTouch, then naturally it'll only work on a device, since desktop computers don't have 'touches'.

avatar image
0

Answer by syclamoth · Oct 16, 2011 at 03:45 PM

The << operator is a bit-shift- it is pretty much the same in every language, although it is not exactly the most common of operations. For things like layer masks, (which are implemented as bitmasks), it can be very useful! Look up bitwise operations, it'll be enlightening for you.. As for the raycasting, it's pretty straightforward- the script uses a raycast in world space to translate from a (2d) position on the screen into a (3d) position in the world! This is because any given position on the screen could be referring to something at any depth- therefore, the raycast detects the uppermost object, and makes the script act on that. Also, I doubt that it 'works fine' when you comment out the raycast condition- I'm pretty sure you'll be introducing bugs by removing something that fundamental to the way it works.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disable gui texture in a different way? 0 Answers

Android touch 3d Object event 1 Answer

How to make Headbobber on joystick Android 2 Answers

Make an object move in the direction of touch 0 Answers

How do you Draw a Line Using your Finger's Position on Android 3 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