Heya forumites,
I’m trying to script a bunch of square blocks to instantiate at a random rotation along the y-axis, only at 90 degree increments (so that no matter which rotation value the blocks adopt, they still line up next to each other). When instantiating these blocks, my script draws a random value from an array of 4 values (in degrees): 90, -90, 180, -180, and applies the value to transform.rotation.y.
Here’s the script for reference:
public class BlockSpawn : MonoBehaviour
{
public GameObject[] blockPrefab;
public GameObject blockTemplate; // This is just a visual mesh used so that I can see the object in the editor
public float[] rotationAngle;
// Use this for initialization
void Awake ()
{
Destroy(blockTemplate); // Destroying the visual reference mesh prior to instantiating square blocks.
// Rotate the BlockSpawner at a random 90 degree angle, before instantiating block.
Quaternion rotation = transform.rotation;
rotation.y = Random.Range(0, rotationAngle.Length);
transform.rotation = rotation;
// Instantiate a random block prefab from a list, inheriting the rotation of the BlockSpawner.
GameObject block = blockPrefab[Random.Range(1, blockPrefab.Length)];
Instantiate(block, transform.position, transform.rotation);
}
}
The problem: Some blocks end up instantiating at a 45 degree angle instead (ie. they end up diagonal and clipping into their square neighbour). It’s as if the engine is trying to rotate the block a full 90 degree increment, but stops at the halfway mark.
The weird thing is, if I reduce the array in the inspector to 2 possible angles (eg. 90, -90), the blocks spawn at the correct angles. However, increasing the array size to 3 possible angles and some blocks end up spawning diagonally (even if the array is, say, 90, 90, 90 ie. all identical angles).
In a nutshell, increasing the array size of possible values above 2 will result in the blocks not rotating fully prior to instantiation.
Anyone have any idea what might be going on here? Possible fixes/workarounds would be much appreciated!