x


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

more ▼

asked Oct 16, 2011 at 03:30 PM

smtabatabaie gravatar image

smtabatabaie
240 56 47 51

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 16, 2011 at 03:40 PM

superpig gravatar image

superpig ♦
1.2k 24 25 39

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?

Oct 16, 2011 at 06:53 PM smtabatabaie

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

Oct 16, 2011 at 08:08 PM smtabatabaie

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

Oct 16, 2011 at 11:38 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 16, 2011 at 03:45 PM

syclamoth gravatar image

syclamoth
18.3k 19 26 110

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5986
x4564
x2719
x1119
x87

asked: Oct 16, 2011 at 03:30 PM

Seen: 3383 times

Last Updated: Oct 16, 2011 at 11:38 PM