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