- Home /
What's the best way to check if an area is empty?
I have an object which I need to reposition randomly.
I need to make sure I don't position it inside parts of the landscape, which is also generated randomly.
OnTriggerNull would be nice, but there's no such function.
Any ideas?
Answer by Graham-Dunnett · May 26, 2011 at 01:21 PM
Choose a x and a z position randomly, then fire a ray from high up in y downwards. Place the object where the ray hits the landscape. (Assuming your world has y pointing upwards.)
He beat me to it. $$anonymous$$y first post went to another question, so I had to delete it, then repost it.
Answer by almo · May 26, 2011 at 01:21 PM
You could pick a random X,Z location, and pick a Y location higher than anything in the landscape, and do a raycast down from there. Then place the object where the raycast hits.
Answer by Recluse · May 26, 2011 at 02:11 PM
Thanks Graham - I had thought of using Raycasting, but I need to check a cubic area, which means casting three rays minimum and which would still not be faultless, due to the angled geometry of the landscape. I want to make sure my Pickup object is not partially located in a wall, tree, rock etc.
Currently I am positioning the object and if it collides with something I reposition it until it doesn't - which is a bit clunky. All this happens out of sight of the player, but I'd like to find a really elegant way of doing it. Neither this nor Raycasting is that tidy (would love OnTriggerNull - wouldn't that be useful?)
Hmmmm....
Please use 'comment' not 'answer' to comment on other posts.
Answer by Recluse · May 26, 2011 at 02:17 PM
The landscape is a cavern / tunnel with lots of obstructions. I just need a foolproof way of finding nice empty spots in it. So far can't think of anything simpler than just randomly positioning the object repeatedly until OnTriggerEnter fails to flag a collision.
This might be a weird suggestion but could possibly work. How about an empty spinning and moving gameobject that shoots a ray?
Or iterate through positions in a grid inside the tunnel area.
Answer by homeros · May 26, 2011 at 05:25 PM
How about using an invisible mesh smaller than tunnels to specify areas you can spawn the object. You can use random coordinates from collider.bounds or generate random numbers and check if the collider contains them?