- Home /
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
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.
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?
even when I delete the layermask parameter from Physics.Raycast function , it doesn't work ;(
Oh! If it uses Input.GetTouch, then naturally it'll only work on a device, since desktop computers don't have 'touches'.
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.