# A way to instantiate an item with 90 Degree increments?

I am working on a very complex simulation game, I have code most of it myself, However for things you spawn in such as torches, and unsymmetrical items, I need a way that I can instantiate the prefab and rotate it to face me at 90 degree increments. Such as if I were placing a Cabinet, I don’t want it to be the inverted Identity, because if I’m offset from the wall, It will be offset too. Thus part of the cabinet would be in the wall. The only axis that I want the objects to rotate around is the Y axis. Thanks in advance!

PS. All I need help with is to make the part of the Instantiate(Block, hit.point, ------) The ---- is the rotation part I need help with. I need an equation to put in that will always result in 90 degree increments, to the vector rotation, Im fairly sure some Conversion of Quaternions would be involved or something like that.

Hi @natsirt,

The key here is actually to use integers and the modulo operator (%), which gives you a remainder of the division with a number. An example to show this:

``````113 % 90 = 23
113 - 23 = 90
``````

In that example, you take the remainder of 113/90 and subtract it from 113, getting 90. If you did it with (say) 281 instead, it would return the closest multiple of 90, which is 270. So you can take whatever your angle is (and you can use Unity’s Random class to generate random angles between 0 and 360) and get it to the nearest 90 degrees with something like this:

``````angleIWant = myAngle - (myAngle % 90);
``````

At that point, all you have to do is look through the script reference for Quaternion to see how to turn an angle around the Y axis into a quaternion.

I use mudulo yesterday in this script for AI, thanks folks!

``````// Script to rotate all selected objects by set increments with the intention that it will be useful for the generation of pattern tiles. Copyright runninghead.com 2020. If you use this script please send me examples of your work, I'd love to see what you can do with it! Any suggestions / bug reports welcome.

// NOTE: Does not currently work through Groupings, so Ungroup your objects before running this script!

nSel = aSel.length;// number of objects currently selected in illustrator
increment = Number(prompt("Rotation degree increments (30, 45, 60, 90, etc?)", 60));

for(i=0; i<nSel; i++)
{
randomAngle = Math.floor(Math.random()*360);
rotationAngle = randomAngle - (randomAngle % increment);
aSel*.rotate(rotationAngle, rotationAngle);*
``````

}
, // Script to rotate all selected objects by set increments with the intention that it will be useful for the generation of pattern tiles. Copyright runninghead.com 2020. If you use this script please send me examples of your work, I’d love to see what you can do with it! Any suggestions / bug reports welcome.

// NOTE: Does not currently work through Groupings, so Ungroup your objects before running this script!