Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to generate random objects at different locations on x, y, z axis
I have been trying to generate a random object on all three axis in different locations. However, Vector3 doesn't accept that many overloads e.g:
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
I want a spawn limit range for y and z similar to x in the above example but I dont know how to impliment it. The results being that I can generate an object within a positive and negative range on y and z as well?
1 answer
Since the Vector3
constructor accepts separate X, Y and Z coordinates, if you want to randomise each one individually, you would need to call the Random.Range
for each coordinate, e.g.
Vector3 spawnPos = new Vector3(
Random.Range(spawnLimitXLeft, spawnLimitXRight),
Random.Range(spawnLimitYBottom, spawnLimitYTop),
Random.Range(spawnLimitZMin, spawnLimitZMax)
);
1 comment thread