- Home /
How to make an object/prefab choose between multiple given positions to spawn into?
I have an object/prefab (redcube) and I want to spawn another bluecube next to the red cube, so that only one of it's sides touches the other's. The cube has 6 sides, so I can create 6 Vector3 positions for the bluecube to spawn into. How can I make the bluecube randomly choose one of those 6 positions to spawn into?
Comment
Answer by Captain_Pineapple · Sep 23, 2020 at 11:44 AM
i guess this here would be the easiest solution:
private readonly Vector3[] directions =
{
Vector3.forward,
Vector3.back,
Vector3.up,
Vector3.down,
Vector3.right,
Vector3.left
};
private Vector3 getRandomDirection()
{
return directions[UnityEngine.Random.Range(0, directions.Length)];
}
let me know if something is missing/unclear.
Note that this assumes that your cubes are aligned with the world coordinates since you did not specify this any further.